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