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 +463 -251 2.1.1trunk View file →
@@ -1,86 +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.1.1
7 - * Author: Taboola
3 + * Plugin Name: Taboola
4 + * Plugin URI: https://developers.taboola.com/web-integrations/docs/wordpress-plugin
5 + * Description: Taboola
6 + * Version: 3.1.0
7 + * Author: Taboola
8 8 */
9 9
10 -define ("TABOOLA_PLUGIN_VERSION","2.1.1"); // => UPDATE THIS FOR *EVERY* RELEASE (USED FOR TRACKING)
11 -define ("TABOOLA_MIN_VER","2.0.1"); // => 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.1.0' ); // 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;
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;
35 44
36 - function __construct()
37 - {
38 - global $wpdb;
45 + public $plugin_name;
46 + public $plugin_directory;
47 + public $plugin_url;
48 + public $settings;
49 + public $tbl_taboola_settings;
39 50
40 - //initialize plugin constant
41 - DEFINE('TaboolaWP', true);
51 + /* ─────────────────────────── constructor ─────────────────────────── */
52 + public function __construct() {
53 + global $wpdb;
54 + //initialize plugin constant
55 + define( 'TaboolaWP', true );
42 56
43 - $this->_is_widget_on_page = false;
44 - $this->_is_head_script_loaded = false;
57 + $this->_is_widget_on_page = false;
58 + $this->_is_head_script_loaded = false;
45 59
46 - $this->plugin_name = plugin_basename(__FILE__);
47 - $this->plugin_directory = plugin_dir_path(__FILE__);
48 - $this->plugin_url = trailingslashit(WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__)));
49 - $this->settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
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 + );
50 69
51 - $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings';
70 + /* activation / upgrade */
71 + register_activation_hook( $this->plugin_name, [ $this, 'update_db' ] );
72 + add_action( 'admin_init', [ $this, 'update_db' ] );
52 73
53 - //activation function
54 - register_activation_hook($this->plugin_name, array(&$this, 'update_db'));
55 - add_action('admin_init', array(&$this, 'update_db'));
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 + }
56 81
57 - // Enable sidebar widgets
58 - if ($this->settings != NULL && !empty($this->settings->publisher_id)){
59 - //register Taboola widget
60 - add_action('widgets_init',
61 - function(){
62 - return register_widget("WP_Widget_Taboola");
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 + }
63 112 }
64 - );
65 - }
113 + add_action( 'wp_footer', [ $this, 'taboola_footer_loader_js' ] );
66 114
67 - //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled;
115 + /* content widgets */
116 + add_filter( 'the_content', [ $this, 'load_taboola_content' ] );
117 + add_filter( 'the_content', [ $this, 'load_taboola_content_mid' ] );
68 118
69 - if (is_admin()) {
70 - //add menu for plugin
71 - add_action( 'admin_menu', array(&$this, 'admin_generate_menu') );
72 - add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
73 - }else if ($this->settings != NULL){
74 - add_action('wp_head', array(&$this, 'taboola_header_loader_inject'));
75 - add_action('wp_footer', array(&$this, 'taboola_footer_loader_js'));
76 - add_filter('the_content', array(&$this, 'load_taboola_content'));
77 - add_filter('the_content', array(&$this, 'load_taboola_content_mid'));
78 - add_filter('the_content', array(&$this, 'load_taboola_content_home'));
79 - }
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' ] );
80 147 }
148 +}
149 +public function tb_home_buffer_inject( $html ) {
150 + return $this->load_taboola_content_home( $html );
151 +}
81 152
82 - 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) {
83 215 static $this_plugin;
84 216
85 217 if (!$this_plugin) {
86 218 $this_plugin = plugin_basename(__FILE__);
@@ -102,17 +234,20 @@
102 234 private function should_show_content_widget_mid(){
103 235 // PC - only if v2 was installed:
104 236 if (!$this->is_db_updated_for_min_ver("2.0.0"))
105 237 return false;
106 - $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));
107 239 return $retVal1;
108 240 }
109 241
110 242 private function should_show_content_widget_home(){
111 243 // PC - only if v2 was installed:
244 +
245 + //error_log( print_r( $this->settings, true ) ); // TEMP line
246 +
112 247 if (!$this->is_db_updated_for_min_ver("2.0.0"))
113 248 return false;
114 - $retVal2 = ((trim($this->settings->publisher_id) != '') && is_front_page() && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
249 + $retVal2 = ((trim($this->settings->publisher_id) != '') && (is_front_page() || is_home()) && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
115 250 return $retVal2;
116 251 }
117 252
118 253 private function should_show_sidebar_widget(){
@@ -121,9 +256,9 @@
121 256 }
122 257
123 258 // Determine if a taboola widget should be added somewhere on the current page (content or sidebar)
124 259 function is_widget_on_page(){
125 - 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() ;
126 261 }
127 262
128 263 function get_page_type(){
129 264 $page_type='article';
@@ -135,15 +270,21 @@
135 270 return $page_type;
136 271 }
137 272
138 273 // return the head loader script
139 - function taboola_header_loader_js() {
140 - $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 + }
141 286
142 - // Only adding the loader if a widget is going to be placed on the page.
143 - if ($this->is_widget_on_page()){
144 - // PC - since these params will be inserted in 'loaderInjectionScript.js' via search and replace,
145 - // double brackets are used to ensure that each key is a unique string.
146 287 $stringParams = array(
147 288 '{{PUBLISHER_ID}}' => $this->settings->publisher_id,
148 289 '{{PAGE_TYPE}}' => $this->get_page_type(),
149 290 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
@@ -148,30 +289,38 @@
148 289 '{{PAGE_TYPE}}' => $this->get_page_type(),
149 290 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
150 291 '{{PHP_VERSION}}' => phpversion(),
151 292 '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION,
152 - '{{LOC_MID}}' => $this->settings->mid_location_string,
153 - '{{LOC_HOME}}' => $this->settings->home_location_string
293 + '{{LOC_MID}}' => $mid_locations_string,
294 + '{{LOC_HOME}}' => $this->settings->home_location_string ?? ''
154 295 );
155 296
156 297 $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
157 - $head_string = $scriptWrapper->getScriptMarkupString();
298 + return $scriptWrapper->getScriptMarkupString();
158 299 }
159 -
160 - return $head_string;
300 + return "";
161 301 }
162 302
163 -
164 303 // This function is used for the hook action, injects the header content to the <head> tag.
165 304 function taboola_header_loader_inject(){
166 305 echo $this->taboola_header_loader_js();
167 306 }
168 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 + }
169 318
170 319 function taboola_footer_loader_js() {
171 320
172 321 // Only adding flush script if a widget is going to be placed on the page.
173 - if ( $this->is_widget_on_page() ){
322 + if ($this->is_widget_on_page()){
174 323 $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array());
175 324 echo $flushInjectionScript->getScriptMarkupString();
176 325 }
177 326 }
@@ -199,24 +348,37 @@
199 348 return $content;
200 349 }
201 350
202 351 // Mid-article-widget
203 - function load_taboola_content_mid($content)
204 - {
205 - $taboola_content_mid = array();
352 + function load_taboola_content_mid($content) {
206 353 if ($this->should_show_content_widget_mid()){
354 + $mid_widgets = json_decode($this->settings->mid_widgets ?? '[]');
207 355
208 - $secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->mid_widget_id,
209 - '{{CONTAINER}}' => 'taboola-mid-article-thumbnails',
210 - '{{PLACEMENT}}' => $this->settings->mid_placement);
211 -
212 - $secondWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$secondWidgetParams);
213 - $taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-mid-article-thumbnails'></div>";
214 - $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 + );
215 365
216 - $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 + }
217 380 }
218 -
219 381 return $content;
220 382 }
221 383
222 384 // Homepage widget
@@ -221,9 +383,9 @@
221 383
222 384 // Homepage widget
223 385 function load_taboola_content_home($content)
224 386 {
225 -
387 + //error_log( 'TB-DEBUG ② entered load_taboola_content_home' );
226 388 $taboola_content_home = array();
227 389 if ($this->should_show_content_widget_home()){
228 390
229 391 $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id,
@@ -238,9 +400,33 @@
238 400 }
239 401 return $content;
240 402
241 403 }
404 + /* ------------------------------------------------------------------
405 + * CATEGORY ▸ build widget & inject
406 + * ------------------------------------------------------------------*/
407 +public function load_taboola_content_category( $content ) {
242 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 +
243 429 // Below-article widget
244 430
245 431 // Extract the taboola content in the required format:
246 432 // String - for injecting on the servr side
@@ -307,8 +493,41 @@
307 493 return $ret_val;
308 494
309 495 }
310 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 +
311 530 // Below-article widget
312 531 // Do the actual logic of choosing where to place the taboola content.
313 532 function embed_taboola_content_location($content, $taboola_content){
314 533 $do_default = true;
@@ -316,13 +535,13 @@
316 535 // tag is placed outside of content in order to allow "read more" functionality.
317 536 if ($this->settings->out_of_content_enabled){
318 537
319 538 $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array(
320 - "{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML),
321 - "{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT))
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)))
322 541 );
323 542 $scriptWrapper->appendScript("injectWidgetByMarker('tbmarker');");
324 - $content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$scriptWrapper."</script>";
543 + $content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$this->wpautop_safe_script($scriptWrapper)."</script>";
325 544 $do_default = false;
326 545 }
327 546
328 547 // Default for below-article widget - add to the end of the content
@@ -334,9 +553,9 @@
334 553 }
335 554
336 555 // Mid-article widget
337 556 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
338 - function embed_taboola_content_location_mid($content, $taboola_content_mid, $location){
557 + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location, $occurrence = 1){
339 558 $do_default = true;
340 559
341 560 if (isset($location) && $location != ''){
342 561 $first_char = substr($location,0,1);
@@ -348,13 +567,13 @@
348 567 if ($full_indicator == TABOOLA_JS_INDICATOR){
349 568
350 569 $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR));
351 570 $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array(
352 - "{{HTML}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_HTML),
353 - "{{SCRIPT}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_SCRIPT))
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)))
354 573 );
355 574 $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');");
356 - $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>";
575 + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$this->wpautop_safe_script($scriptWrapper)."</script>";
357 576
358 577 $do_default = false;
359 578 }
360 579
@@ -360,12 +579,14 @@
360 579
361 580 // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
362 581 // basically it's CSS selectors like in jQuery
363 582 } else{
364 - require_once('simple_html_dom.php');
583 + if ( ! class_exists( 'simple_html_dom' ) ) {
584 + require_once('simple_html_dom.php');
585 + }
365 586
366 587 $html_doc = str_get_html($content);
367 - $target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1);
588 + $target_location = $html_doc->find($location, ($occurrence) - 1);
368 589
369 590 // if the location was found within the html content
370 591 if (isset($target_location) && is_object($target_location)){
371 592
@@ -375,12 +596,8 @@
375 596 $do_default = false;
376 597 }
377 598 }
378 599 }
379 - // Default for below-article widget - add to the end of the content
380 - // if ($do_default){
381 - // $content = $content.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING);
382 - // }
383 600
384 601 return $content;
385 602 }
386 603
@@ -387,8 +604,22 @@
387 604 // Homepage widget
388 605 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
389 606 function embed_taboola_content_location_home($content, $taboola_content_home, $location){
390 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 + // ────────────────────────────
391 622 $do_default = true;
392 623
393 624 if (isset($location) && $location != ''){
394 625 $first_char = substr($location,0,1);
@@ -400,13 +631,13 @@
400 631 if ($full_indicator == TABOOLA_JS_INDICATOR){
401 632
402 633 $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR));
403 634 $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array(
404 - "{{HTML}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_HTML),
405 - "{{SCRIPT}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_SCRIPT))
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)))
406 637 );
407 638 $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');");
408 - $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>";
639 + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$this->wpautop_safe_script($scriptWrapper)."</script>";
409 640
410 641 $do_default = false;
411 642 }
412 643
@@ -411,10 +642,12 @@
411 642 }
412 643
413 644 // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
414 645 // basically it's CSS selectors like in jQuery
415 - } else{
646 + } else{
647 + if ( ! class_exists( 'simple_html_dom' ) ) {
416 648 require_once('simple_html_dom.php');
649 + }
417 650
418 651 $html_doc = str_get_html($content);
419 652 $target_location = $html_doc->find($location,($this->settings->home_location_string_occurrence)-1);
420 653
@@ -428,25 +661,32 @@
428 661 }
429 662 }
430 663 }
431 664 // Default for below-article widget - add to the end of the content
432 - // if ($do_default){
433 - // $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
434 - // }
665 + if ($do_default){
666 + $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
667 + }
435 668 return $content;
669 +
436 670
437 671 }
438 672
439 673 function admin_generate_menu(){
440 674 global $current_user;
441 - 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);
442 676 }
443 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 +
444 684 function admin_taboola_settings(){
445 -
446 685 global $wpdb;
447 686 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
448 687 $taboola_errors = array();
688 + $taboola_save_error = '';
449 689 if($_SERVER['REQUEST_METHOD'] == 'POST'){
450 690
451 691 if(trim(strip_tags($_POST['publisher_id'])) == ''){
452 692 $taboola_errors[] = "Publisher ID";
@@ -451,8 +691,14 @@
451 691 if(trim(strip_tags($_POST['publisher_id'])) == ''){
452 692 $taboola_errors[] = "Publisher ID";
453 693 }
454 694
695 + if(isset($_POST['web_push_enabled'])) {
696 + if (trim(strip_tags($_POST['publisher_id_push'])) == '') {
697 + $taboola_errors[] = "Publisher Push ID";
698 + }
699 + }
700 +
455 701 if(isset($_POST['first_bc_enabled'])) {
456 702 if (trim(strip_tags($_POST['first_bc_widget_id'])) == '') {
457 703 $taboola_errors[] = "Below-article > Widget ID";
458 704 }
@@ -461,33 +707,18 @@
461 707 }
462 708 }
463 709
464 710 if(isset($_POST['mid_enabled'])) {
465 - if (trim(strip_tags($_POST['mid_widget_id'])) == '') {
466 - $taboola_errors[] = "Mid-article > Widget ID";
467 - }
468 - if (trim(strip_tags($_POST['mid_placement'])) == '') {
469 - $taboola_errors[] = "Mid-article > Placement Name";
470 - }
471 - if (trim(strip_tags($_POST['mid_location_string'])) == '') {
472 - $taboola_errors[] = "Mid-article > CSS selector";
473 - }
474 - else {
475 - // If the 'location' WAS filled in, then...
476 -
477 - // 1) Check for a valid 'location':
478 -
479 - // Validation method has not been implemented
480 - // mid_if (!$this->is_locaton_string_valid1($_POST['mid_location_string'])) {
481 - // $taboola_errors[] = "Mid-article > CSS Selector (invalid value)";
482 - // }
483 -
484 - // 2) Validate the 'occurrence':
485 - if (!$this->is_mid_location_string_occurrence_valid($_POST['mid_location_string_occurrence'])) {
486 - $taboola_errors[] = "Mid-article > Occurance (must be >= 1)";
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 + }
487 719 }
488 720 }
489 -
490 721 }
491 722
492 723 if(isset($_POST['home_enabled'])) {
493 724 if (trim(strip_tags($_POST['home_widget_id'])) == '') {
@@ -500,53 +731,79 @@
500 731 if (trim(strip_tags($_POST['home_location_string'])) == '') {
501 732 $taboola_errors[] = "Homepage > CSS selector";
502 733 }
503 734 else {
504 - // If the 'location' WAS filled in, then...
505 -
506 - // 1) Check for a valid 'location':
507 -
508 - // Validation method has not been implemented
509 - // if (!$this->is_home_location_string_valid($_POST['home_location_string'])) {
510 - // $taboola_errors[] = "Homepage > CSS Selector (invalid value)";
511 - // }
512 -
513 - // 2) Validate the 'occurrence':
514 735 if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) {
515 736 $taboola_errors[] = "Homepage > Occurance (must be >= 1)";
516 737 }
517 738 }
518 - }
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 + }
519 746
520 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 + }
521 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. */
522 777 $data = array(
523 778 "publisher_id" => trim($_POST['publisher_id']),
524 779
525 - "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false,
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,
526 784 "first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '',
527 785 "first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '',
528 786
529 - "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false,
787 + "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? 1 : 0,
530 788
531 - "mid_enabled" => isset($_POST['mid_enabled']) ? true : false,
532 - "mid_widget_id" => !empty($_POST['mid_widget_id']) ? trim($_POST['mid_widget_id']) : '',
533 - "mid_placement" => !empty($_POST['mid_placement']) ? trim($_POST['mid_placement']) : '',
534 - "mid_paragraph_ui_mode" => !empty($_POST['mid_paragraph_ui_mode']) ? trim($_POST['mid_paragraph_ui_mode']) : '',
535 -
536 - "mid_location_string_occurrence" => !empty($_POST['mid_location_string_occurrence']) ? $_POST['mid_location_string_occurrence'] : '',
537 - "mid_location_string" => !empty($_POST['mid_location_string']) ? trim($_POST['mid_location_string']) : '',
538 -
539 - "home_enabled" => isset($_POST['home_enabled']) ? true : false,
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,
540 793 "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '',
541 794 "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '',
542 795
543 - "home_location_string_occurrence" => !empty($_POST['home_location_string_occurrence']) ? $_POST['home_location_string_occurrence'] : '',
544 - "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : ''
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 +
545 804 );
546 805
547 - //var_dump($settings);
548 -
549 806 $is_valid_nonce = false;
550 807
551 808 if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) {
552 809 $is_valid_nonce = true;
@@ -553,21 +810,27 @@
553 810 }
554 811
555 812 if ($is_valid_nonce) {
556 813 if($settings == NULL){
557 - $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
814 + $saved = $wpdb->insert($this->tbl_taboola_settings, $data);
558 815 } else {
559 - $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
816 + $saved = $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
560 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 + }
824 + } else {
825 + $taboola_save_error = "Security check failed - the settings page had been open too long. Reload it and apply your changes again.";
561 826 }
562 -
563 827 }
564 828 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
565 829 }
566 -
567 830 include_once('settings.php');
568 831 }
569 -
832 +
570 833 function is_mid_location_string_valid($mid_location_string){
571 834 // TODO:: validate the location string
572 835 return true;
573 836 }
@@ -591,8 +854,9 @@
591 854 return true;
592 855 }
593 856 return false;
594 857 }
858 +
595 859 function columnExists($column_name) {
596 860 global $wpdb;
597 861 $table_name = $wpdb->prefix . "_taboola_settings";
598 862 tb_write_log("table name : " . $table_name);
@@ -618,25 +882,12 @@
618 882 else {
619 883 tb_write_log("This is NOT a v1 upgrade!"); //PC
620 884 return false;
621 885 }
622 -
623 886 }
624 887
625 888 function is_db_updated_for_min_ver($min_ver) {
626 - /**
627 - * Checks if the DB was *previously* updated for the minimum version specified.
628 - * $min_ver should be passed in a format like this: "2.1.0"
629 - */
630 -
631 889 global $wpdb;
632 -
633 - // PC - Temporary patch for v2.1.0
634 -
635 - // START patch ===============================================
636 - // Check if the `_taboola` table exists.
637 - // If not, then return false.
638 -
639 890 $table_name = $wpdb->prefix . "_taboola_settings";
640 891
641 892 if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
642 893 tb_write_log("Table NOT FOUND");
@@ -644,55 +895,31 @@
644 895 }
645 896 else {
646 897 tb_write_log("Table EXISTS");
647 898 }
648 - // END patch =================================================
649 -
650 - // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
651 899 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
652 900
653 901 $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
654 - //tb_write_log($db_is_up_to_date);
655 902 return $db_is_up_to_date;
656 903
657 904 }
658 905
659 906 function is_db_updated_for_current_ver() {
660 - /**
661 - * DEPRECATED
662 - * Checks if the DB was *previously* updated for the version currently loaded.
663 - */
664 907 global $wpdb;
665 -
666 - // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
667 908 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
668 -
669 - // Get the currently loaded plugin version.
670 909 $plugin_version = $this->get_loaded_plugin_version();
671 -
672 910 $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
673 - //tb_write_log($db_is_up_to_date);
674 911 return $db_is_up_to_date;
675 -
676 912 }
677 913
678 914 function get_loaded_plugin_version() {
679 -
680 - $plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only
915 + $plugin_data = get_plugin_data( __FILE__ );
681 916 $loaded_plugin_version = $plugin_data['Version'];
682 917 return $loaded_plugin_version;
683 -
684 918 }
685 919
686 920 function save_taboola_version($min_ver) {
687 - /**
688 - * Checks for the saved version in wp_options.
689 - * If not found - adds it. Else - updates it.
690 - */
691 -
692 -
693 921 tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC
694 -
695 922 $saved_version = get_option(TABOOLA_OPTION_NAME, '');
696 923 if ($saved_version == '') {
697 924 add_option(TABOOLA_OPTION_NAME, $min_ver);
698 925 }
@@ -701,12 +928,9 @@
701 928 }
702 929 }
703 930
704 931 function update_db(){
705 -
706 - // If we are up to date, then skip this method...
707 932 if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
708 - //tb_write_log("All up to date!");
709 933 return;
710 934 }
711 935
712 936 $this->save_taboola_version(TABOOLA_MIN_VER);
@@ -713,54 +937,41 @@
713 937
714 938 global $wpdb;
715 939 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
716 940
717 -
718 - // Handling for older MySQL versions
719 - if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
720 - if (!empty($wpdb->charset))
721 - $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
722 - if (!empty($wpdb->collate))
723 - $charset_collate .= " COLLATE $wpdb->collate";
941 + $charset_collate = '';
942 + if ( ! empty( $wpdb->charset ) ) {
943 + $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
724 944 }
945 + if ( ! empty( $wpdb->collate ) ) {
946 + $charset_collate .= " COLLATE {$wpdb->collate}";
947 + }
725 948
726 - // Check if this is an upgrade from v1
727 949 $is_upgrade_from_v1 = $this->is_upgrade_from_v1();
728 -
729 - // tb_write_log("Is upgrade from v1: " . ($is_upgrade_from_v1 ? 'true' : 'false'));
730 -
731 -
732 - //settings table structure
733 950 $sql_table_settings = "
734 951 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
735 952 `id` INT NOT NULL AUTO_INCREMENT ,
736 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,
737 956 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
738 957 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
739 958 `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .",
740 - `first_bc_custom_css` TEXT DEFAULT NULL,
741 - `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
742 - `second_bc_widget_id` VARCHAR(255) DEFAULT NULL,
743 - `second_bc_custom_css` TEXT DEFAULT NULL,
744 - `location_string` TEXT DEFAULT NULL,
959 + `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
745 960 `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
746 - `mid_widget_id` VARCHAR(255) DEFAULT NULL,
747 - `mid_placement` VARCHAR(255) DEFAULT NULL,
748 - `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
749 - `mid_location_string` TEXT DEFAULT NULL,
750 - `mid_location_string_occurrence` SMALLINT DEFAULT NULL,
751 - `mid_paragraph_ui_mode` VARCHAR(255) DEFAULT NULL,
961 + `mid_widgets` TEXT DEFAULT NULL,
752 962 `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
753 963 `home_widget_id` VARCHAR(255) DEFAULT NULL,
754 964 `home_placement` VARCHAR(255) DEFAULT NULL,
755 965 `home_location_string` TEXT DEFAULT NULL,
756 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,
757 972 PRIMARY KEY (`id`)
758 - )" . $charset_collate . ";";
759 -
760 - // tb_write_log($sql_table_settings);
761 -
762 - // create/update the table
973 + ) $charset_collate;";
763 974 dbDelta($sql_table_settings);
764 975 }
765 976 }
766 977 }
@@ -767,18 +978,13 @@
767 978
768 979 global $taboolaWP;
769 980 $taboolaWP = new TaboolaWP();
770 981
771 -/*
772 -A few utility methods for logging
773 -*/
774 -
775 -// Writes debugging messages to 'logs/debug.log'.
776 -// The exact file location depends on *where* you trigger the function.
777 -// In this script, we trigger the function from the admin dashboard.
778 -// So the log file is located under 'wp-admin/logs'.
779 982 function tb_write_log($log_msg)
780 983 {
984 + if (!TABOOLA_DEBUG_MODE)
985 + return;
986 +
781 987 $log_filename = "logs";
782 988 if (!file_exists($log_filename))
783 989 {
784 990 mkdir($log_filename, 0777, true);
@@ -784,18 +990,14 @@
784 990 mkdir($log_filename, 0777, true);
785 991 }
786 992 $log_file_data = $log_filename.'/debug.log';
787 993
788 - // Add date/time
789 994 $date = date('Y-m-d H:i:s');
790 995 $log_msg_with_date = $date." : ".$log_msg;
791 -
792 - if (TABOOLA_DEBUG_MODE)
793 - file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
794 -
996 +
997 + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
795 998 }
796 999
797 -// Write to console
798 1000 function tb_console_log( $data ){
799 1001
800 1002 If(wp_get_environment_type() === 'development') {
801 1003
@@ -805,9 +1007,8 @@
805 1007
806 1008 }
807 1009 }
808 1010
809 -// Write to webpage and stop
810 1011 function tb_print_to_page($data) {
811 1012 If(wp_get_environment_type() === 'development') {
812 1013 print_r($data);
813 1014 die;
@@ -813,5 +1014,16 @@
813 1014 die;
814 1015 }
815 1016 }
816 1017
817 -//
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');