PluginProbe
Taboola / 3.0.2
Taboola v3.0.2
1.0.4 1.0.5 1.0.6 1.0.8 2.0.1 2.0.2 2.1.0 2.1.1 2.2.2 2.2.3 3.0.0 3.0.1 3.0.2 3.1.0 trunk 1.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.2 1.0.3
← All changes | taboola_widget.php +430 -227 2.0.13.0.2 View file →
@@ -1,85 +1,218 @@
1 1 <?php
2 2 /**
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
3 + * Plugin Name: Taboola
4 + * Plugin URI: https://developers.taboola.com/web-integrations/docs/wordpress-plugin
5 + * Description: Taboola
6 + * Version: 3.0.2
7 + * Author: Taboola
8 8 */
9 9
10 -define ("TABOOLA_PLUGIN_VERSION","2.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)
10 +define( 'TABOOLA_PLUGIN_VERSION', '3.0.2' ); // track every release
11 +define( 'TABOOLA_MIN_VER', '3.0' ); // bump only when DB changes
12 +define( 'TABOOLA_DEBUG_MODE', false );
13 13
14 -define ("TABOOLA_OPTION_NAME","taboola_plugin_version"); // *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
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;
32 + /* ─────────────────────────── properties ─────────────────────────── */
33 + public $data = [];
34 + public $_is_widget_on_page;
35 + public $_is_head_script_loaded = false;
34 36
35 - function __construct()
36 - {
37 - global $wpdb;
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;
38 44
39 - //initialize plugin constant
40 - DEFINE('TaboolaWP', true);
45 + public $plugin_name;
46 + public $plugin_directory;
47 + public $plugin_url;
48 + public $settings;
49 + public $tbl_taboola_settings;
41 50
42 - $this->_is_widget_on_page = false;
43 - $this->_is_head_script_loaded = false;
51 + /* ─────────────────────────── constructor ─────────────────────────── */
52 + public function __construct() {
53 + global $wpdb;
54 + //initialize plugin constant
55 + define( 'TaboolaWP', true );
44 56
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");
57 + $this->_is_widget_on_page = false;
58 + $this->_is_head_script_loaded = false;
49 59
50 - $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings';
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 + );
51 69
52 - //activation function
53 - register_activation_hook($this->plugin_name, array(&$this, 'update_db'));
54 - add_action('admin_init', array(&$this, 'update_db'));
70 + /* activation / upgrade */
71 + register_activation_hook( $this->plugin_name, [ $this, 'update_db' ] );
72 + add_action( 'admin_init', [ $this, 'update_db' ] );
55 73
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");
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 + }
62 112 }
63 - );
64 - }
113 + add_action( 'wp_footer', [ $this, 'taboola_footer_loader_js' ] );
65 114
66 - //$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' ] );
67 118
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 - }
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' ] );
79 147 }
148 +}
149 +public function tb_home_buffer_inject( $html ) {
150 + return $this->load_taboola_content_home( $html );
151 +}
80 152
81 - 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) {
82 215 static $this_plugin;
83 216
84 217 if (!$this_plugin) {
85 218 $this_plugin = plugin_basename(__FILE__);
@@ -101,17 +234,20 @@
101 234 private function should_show_content_widget_mid(){
102 235 // PC - only if v2 was installed:
103 236 if (!$this->is_db_updated_for_min_ver("2.0.0"))
104 237 return false;
105 - $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));
106 239 return $retVal1;
107 240 }
108 241
109 242 private function should_show_content_widget_home(){
110 243 // PC - only if v2 was installed:
244 +
245 + //error_log( print_r( $this->settings, true ) ); // TEMP line
246 +
111 247 if (!$this->is_db_updated_for_min_ver("2.0.0"))
112 248 return false;
113 - $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) != '');
114 250 return $retVal2;
115 251 }
116 252
117 253 private function should_show_sidebar_widget(){
@@ -120,9 +256,9 @@
120 256 }
121 257
122 258 // Determine if a taboola widget should be added somewhere on the current page (content or sidebar)
123 259 function is_widget_on_page(){
124 - 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() ;
125 261 }
126 262
127 263 function get_page_type(){
128 264 $page_type='article';
@@ -134,13 +270,20 @@
134 270 return $page_type;
135 271 }
136 272
137 273 // return the head loader script
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.
274 + function taboola_header_loader_js() {
142 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 + }
143 286
144 287 $stringParams = array(
145 288 '{{PUBLISHER_ID}}' => $this->settings->publisher_id,
146 289 '{{PAGE_TYPE}}' => $this->get_page_type(),
@@ -146,30 +289,38 @@
146 289 '{{PAGE_TYPE}}' => $this->get_page_type(),
147 290 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
148 291 '{{PHP_VERSION}}' => phpversion(),
149 292 '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION,
150 - '{{LOC_MID}}' => $this->settings->mid_location_string,
151 - '{{LOC_HOME}}' => $this->settings->home_location_string
293 + '{{LOC_MID}}' => $mid_locations_string,
294 + '{{LOC_HOME}}' => $this->settings->home_location_string ?? ''
152 295 );
153 296
154 297 $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
155 - $head_string = $scriptWrapper->getScriptMarkupString();
298 + return $scriptWrapper->getScriptMarkupString();
156 299 }
157 -
158 - return $head_string;
300 + return "";
159 301 }
160 302
161 -
162 303 // This function is used for the hook action, injects the header content to the <head> tag.
163 304 function taboola_header_loader_inject(){
164 305 echo $this->taboola_header_loader_js();
165 306 }
166 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 + }
167 318
168 319 function taboola_footer_loader_js() {
169 320
170 321 // Only adding flush script if a widget is going to be placed on the page.
171 - if ( $this->is_widget_on_page() ){
322 + if ($this->is_widget_on_page()){
172 323 $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array());
173 324 echo $flushInjectionScript->getScriptMarkupString();
174 325 }
175 326 }
@@ -197,24 +348,37 @@
197 348 return $content;
198 349 }
199 350
200 351 // Mid-article-widget
201 - function load_taboola_content_mid($content)
202 - {
203 - $taboola_content_mid = array();
352 + function load_taboola_content_mid($content) {
204 353 if ($this->should_show_content_widget_mid()){
354 + $mid_widgets = json_decode($this->settings->mid_widgets ?? '[]');
205 355
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;
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 + );
213 365
214 - $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 + }
215 380 }
216 -
217 381 return $content;
218 382 }
219 383
220 384 // Homepage widget
@@ -219,9 +383,9 @@
219 383
220 384 // Homepage widget
221 385 function load_taboola_content_home($content)
222 386 {
223 -
387 + //error_log( 'TB-DEBUG ② entered load_taboola_content_home' );
224 388 $taboola_content_home = array();
225 389 if ($this->should_show_content_widget_home()){
226 390
227 391 $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id,
@@ -236,9 +400,33 @@
236 400 }
237 401 return $content;
238 402
239 403 }
404 + /* ------------------------------------------------------------------
405 + * CATEGORY ▸ build widget & inject
406 + * ------------------------------------------------------------------*/
407 +public function load_taboola_content_category( $content ) {
240 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 +
241 429 // Below-article widget
242 430
243 431 // Extract the taboola content in the required format:
244 432 // String - for injecting on the servr side
@@ -305,8 +493,18 @@
305 493 return $ret_val;
306 494
307 495 }
308 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 +}
309 507 // Below-article widget
310 508 // Do the actual logic of choosing where to place the taboola content.
311 509 function embed_taboola_content_location($content, $taboola_content){
312 510 $do_default = true;
@@ -332,9 +530,9 @@
332 530 }
333 531
334 532 // Mid-article widget
335 533 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
336 - function embed_taboola_content_location_mid($content, $taboola_content_mid, $location){
534 + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location, $occurrence = 1){
337 535 $do_default = true;
338 536
339 537 if (isset($location) && $location != ''){
340 538 $first_char = substr($location,0,1);
@@ -358,12 +556,14 @@
358 556
359 557 // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
360 558 // basically it's CSS selectors like in jQuery
361 559 } else{
362 - require_once('simple_html_dom.php');
560 + if ( ! class_exists( 'simple_html_dom' ) ) {
561 + require_once('simple_html_dom.php');
562 + }
363 563
364 564 $html_doc = str_get_html($content);
365 - $target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1);
565 + $target_location = $html_doc->find($location, ($occurrence) - 1);
366 566
367 567 // if the location was found within the html content
368 568 if (isset($target_location) && is_object($target_location)){
369 569
@@ -373,12 +573,8 @@
373 573 $do_default = false;
374 574 }
375 575 }
376 576 }
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 - // }
381 577
382 578 return $content;
383 579 }
384 580
@@ -385,8 +581,22 @@
385 581 // Homepage widget
386 582 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
387 583 function embed_taboola_content_location_home($content, $taboola_content_home, $location){
388 584
585 +
586 + // error_log( 'TB-DEBUG ③ raw selector string: [' . $location . ']' );
587 +
588 + // load parser
589 + //require_once plugin_dir_path( __FILE__ ) . 'simple_html_dom.php';
590 + // $html_doc = str_get_html( $content );
591 +
592 + // occurrence: settings is 1-based, find() wants 0-based
593 + // $occurrence = max( 1, intval( $this->settings->home_location_string_occurrence ) );
594 + // $node = $html_doc->find( $location, $occurrence - 1 );
595 + // $found = is_object( $node ) ? 1 : 0;
596 +
597 + // error_log( "TB-DEBUG ③ found {$found} matching node(s) for selector [{$location}]" );
598 + // ────────────────────────────
389 599 $do_default = true;
390 600
391 601 if (isset($location) && $location != ''){
392 602 $first_char = substr($location,0,1);
@@ -409,10 +619,12 @@
409 619 }
410 620
411 621 // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm)
412 622 // basically it's CSS selectors like in jQuery
413 - } else{
623 + } else{
624 + if ( ! class_exists( 'simple_html_dom' ) ) {
414 625 require_once('simple_html_dom.php');
626 + }
415 627
416 628 $html_doc = str_get_html($content);
417 629 $target_location = $html_doc->find($location,($this->settings->home_location_string_occurrence)-1);
418 630
@@ -426,18 +638,19 @@
426 638 }
427 639 }
428 640 }
429 641 // Default for below-article widget - add to the end of the content
430 - // if ($do_default){
431 - // $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
432 - // }
642 + if ($do_default){
643 + $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
644 + }
433 645 return $content;
646 +
434 647
435 648 }
436 649
437 650 function admin_generate_menu(){
438 651 global $current_user;
439 - add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110);
652 + 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);
440 653 }
441 654
442 655 function admin_taboola_settings(){
443 656 global $wpdb;
@@ -444,82 +657,95 @@
444 657 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
445 658 $taboola_errors = array();
446 659 if($_SERVER['REQUEST_METHOD'] == 'POST'){
447 660
448 - if(trim($_POST['publisher_id']) == ''){
661 + if(trim(strip_tags($_POST['publisher_id'])) == ''){
449 662 $taboola_errors[] = "Publisher ID";
450 663 }
451 664
665 + if(isset($_POST['web_push_enabled'])) {
666 + if (trim(strip_tags($_POST['publisher_id_push'])) == '') {
667 + $taboola_errors[] = "Publisher Push ID";
668 + }
669 + }
670 +
452 671 if(isset($_POST['first_bc_enabled'])) {
453 - if (trim($_POST['first_bc_widget_id']) == '') {
672 + if (trim(strip_tags($_POST['first_bc_widget_id'])) == '') {
454 673 $taboola_errors[] = "Below-article > Widget ID";
455 674 }
456 - if (trim($_POST['first_bc_placement']) == '') {
675 + if (trim(strip_tags($_POST['first_bc_placement'])) == '') {
457 676 $taboola_errors[] = "Below-article > Placement Name";
458 677 }
459 678 }
460 679
461 680 if(isset($_POST['mid_enabled'])) {
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)";
681 + if(isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) {
682 + foreach ($_POST['mid_widget_id'] as $index => $widget_id) {
683 + if(trim($widget_id) == '') {
684 + $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Widget ID";
685 + }
686 + if(trim($_POST['mid_placement'][$index]) == '') {
687 + $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Placement Name";
688 + }
484 689 }
485 690 }
486 -
487 691 }
488 692
489 693 if(isset($_POST['home_enabled'])) {
490 - if (trim($_POST['home_widget_id']) == '') {
694 + if (trim(strip_tags($_POST['home_widget_id'])) == '') {
491 695 $taboola_errors[] = "Homepage > Widget ID";
492 696 }
493 - if (trim($_POST['home_placement']) == '') {
697 + if (trim(strip_tags($_POST['home_placement'])) == '') {
494 698 $taboola_errors[] = "Homepage > Placement Name";
495 699 }
496 700
497 - if (trim($_POST['home_location_string']) == '') {
701 + if (trim(strip_tags($_POST['home_location_string'])) == '') {
498 702 $taboola_errors[] = "Homepage > CSS selector";
499 703 }
500 704 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':
511 705 if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) {
512 706 $taboola_errors[] = "Homepage > Occurance (must be >= 1)";
513 707 }
514 708 }
515 - }
709 + }
710 + if ( isset($_POST['category_enabled']) ) {
711 + if ( trim(strip_tags($_POST['category_widget_id'])) == '' )
712 + $taboola_errors[] = "Category > Widget ID";
713 + if ( trim(strip_tags($_POST['category_placement'])) == '' )
714 + $taboola_errors[] = "Category > Placement Name";
715 + }
516 716
517 717 if(count($taboola_errors) == 0){
718 +
719 + $mid_widgets_data = array();
720 + if (isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) {
721 + foreach ($_POST['mid_widget_id'] as $index => $widget_id) {
722 + if (!empty(trim($widget_id))) {
723 +
724 + $location_string = 'p';
725 + if (isset($_POST['mid_paragraph_ui_mode'][$index]) && $_POST['mid_paragraph_ui_mode'][$index] === 'Other') {
726 + if (isset($_POST['mid_location_string'][$index])) {
727 + $location_string = sanitize_text_field($_POST['mid_location_string'][$index]);
728 + }
729 + }
518 730
731 + $mid_widgets_data[] = array(
732 + 'widget_id' => sanitize_text_field($widget_id),
733 + 'placement' => sanitize_text_field($_POST['mid_placement'][$index]),
734 + 'location_string' => $location_string,
735 + 'occurrence' => intval($_POST['mid_location_string_occurrence'][$index]),
736 + );
737 + }
738 + }
739 + }
740 + $mid_widgets_json = json_encode($mid_widgets_data);
741 +
519 742 $data = array(
520 743 "publisher_id" => trim($_POST['publisher_id']),
521 744
745 + "web_push_enabled" => isset($_POST['web_push_enabled']) ? true : false,
746 + "publisher_id_push" => !empty($_POST['publisher_id_push']) ? trim($_POST['publisher_id_push']) : '',
747 +
522 748 "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false,
523 749 "first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '',
524 750 "first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '',
525 751
@@ -525,36 +751,43 @@
525 751
526 752 "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false,
527 753
528 754 "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 -
755 + "mid_widgets" => $mid_widgets_json,
756 +
536 757 "home_enabled" => isset($_POST['home_enabled']) ? true : false,
537 758 "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '',
538 759 "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '',
539 760
540 761 "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']) : ''
762 + "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : '',
763 + "category_enabled" => isset($_POST['category_enabled']) ? true : false,
764 + "category_widget_id" => !empty($_POST['category_widget_id']) ? trim($_POST['category_widget_id']) : '',
765 + "category_placement" => !empty($_POST['category_placement']) ? trim($_POST['category_placement']) : '',
766 + "category_location_string_occurrence" => !empty($_POST['category_location_string_occurrence']) ? $_POST['category_location_string_occurrence'] : '',
767 + "category_location_string" => !empty($_POST['category_location_string']) ? trim($_POST['category_location_string']) : '',
768 +
542 769 );
543 770
544 - //var_dump($settings);
545 - if($settings == NULL){
546 - $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
547 - } else {
548 - $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
771 + $is_valid_nonce = false;
772 +
773 + if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) {
774 + $is_valid_nonce = true;
549 775 }
776 +
777 + if ($is_valid_nonce) {
778 + if($settings == NULL){
779 + $wpdb->insert($this->tbl_taboola_settings, $data);
780 + } else {
781 + $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
782 + }
783 + }
550 784 }
551 785 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
552 786 }
553 -
554 787 include_once('settings.php');
555 788 }
556 -
789 +
557 790 function is_mid_location_string_valid($mid_location_string){
558 791 // TODO:: validate the location string
559 792 return true;
560 793 }
@@ -578,8 +811,9 @@
578 811 return true;
579 812 }
580 813 return false;
581 814 }
815 +
582 816 function columnExists($column_name) {
583 817 global $wpdb;
584 818 $table_name = $wpdb->prefix . "_taboola_settings";
585 819 tb_write_log("table name : " . $table_name);
@@ -605,60 +839,44 @@
605 839 else {
606 840 tb_write_log("This is NOT a v1 upgrade!"); //PC
607 841 return false;
608 842 }
609 -
610 843 }
611 844
612 845 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 - */
617 846 global $wpdb;
847 + $table_name = $wpdb->prefix . "_taboola_settings";
618 848
619 - // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
849 + if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
850 + tb_write_log("Table NOT FOUND");
851 + return false;
852 + }
853 + else {
854 + tb_write_log("Table EXISTS");
855 + }
620 856 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
621 857
622 858 $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
623 - //tb_write_log($db_is_up_to_date);
624 859 return $db_is_up_to_date;
625 860
626 861 }
627 862
628 863 function is_db_updated_for_current_ver() {
629 - /**
630 - * Checks if the DB was *previously* updated for the version currently loaded.
631 - */
632 864 global $wpdb;
633 -
634 - // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
635 865 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
636 -
637 - // Get the currently loaded plugin version from wp_options.
638 866 $plugin_version = $this->get_loaded_plugin_version();
639 -
640 867 $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
641 - //tb_write_log($db_is_up_to_date);
642 868 return $db_is_up_to_date;
643 -
644 869 }
645 870
646 871 function get_loaded_plugin_version() {
647 -
648 - $plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only
872 + $plugin_data = get_plugin_data( __FILE__ );
649 873 $loaded_plugin_version = $plugin_data['Version'];
650 874 return $loaded_plugin_version;
651 -
652 875 }
653 876
654 877 function save_taboola_version($min_ver) {
655 - // Get the currently loaded plugin version
656 - // $plugin_version = $this->get_loaded_plugin_version();
657 -
658 878 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.
661 879 $saved_version = get_option(TABOOLA_OPTION_NAME, '');
662 880 if ($saved_version == '') {
663 881 add_option(TABOOLA_OPTION_NAME, $min_ver);
664 882 }
@@ -667,12 +885,9 @@
667 885 }
668 886 }
669 887
670 888 function update_db(){
671 -
672 - // If we are up to date, then skip this method...
673 889 if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
674 - //tb_write_log("All up to date!");
675 890 return;
676 891 }
677 892
678 893 $this->save_taboola_version(TABOOLA_MIN_VER);
@@ -679,54 +894,41 @@
679 894
680 895 global $wpdb;
681 896 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
682 897
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";
898 + $charset_collate = '';
899 + if ( ! empty( $wpdb->charset ) ) {
900 + $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
690 901 }
902 + if ( ! empty( $wpdb->collate ) ) {
903 + $charset_collate .= " COLLATE {$wpdb->collate}";
904 + }
691 905
692 - // Check if this is an upgrade from v1
693 906 $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
699 907 $sql_table_settings = "
700 908 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
701 909 `id` INT NOT NULL AUTO_INCREMENT ,
702 910 `publisher_id` VARCHAR(255) DEFAULT NULL,
911 + `web_push_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
912 + `publisher_id_push` INT(15) DEFAULT NULL,
703 913 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
704 914 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
705 915 `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,
916 + `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
711 917 `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
712 - `mid_widget_id` VARCHAR(255) DEFAULT NULL,
713 - `mid_placement` VARCHAR(255) DEFAULT NULL,
714 - `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE,
715 - `mid_location_string` TEXT DEFAULT NULL,
716 - `mid_location_string_occurrence` SMALLINT DEFAULT NULL,
717 - `mid_paragraph_ui_mode` VARCHAR(255) DEFAULT NULL,
918 + `mid_widgets` TEXT DEFAULT NULL,
718 919 `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
719 920 `home_widget_id` VARCHAR(255) DEFAULT NULL,
720 921 `home_placement` VARCHAR(255) DEFAULT NULL,
721 922 `home_location_string` TEXT DEFAULT NULL,
722 923 `home_location_string_occurrence` SMALLINT DEFAULT NULL,
924 + `category_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
925 + `category_widget_id` VARCHAR(255) DEFAULT NULL,
926 + `category_placement` VARCHAR(255) DEFAULT NULL,
927 + `category_location_string` TEXT DEFAULT NULL,
928 + `category_location_string_occurrence` SMALLINT DEFAULT NULL,
723 929 PRIMARY KEY (`id`)
724 - )" . $charset_collate . ";";
725 -
726 - // tb_write_log($sql_table_settings);
727 -
728 - // create/update the table
930 + ) $charset_collate;";
729 931 dbDelta($sql_table_settings);
730 932 }
731 933 }
732 934 }
@@ -733,18 +935,13 @@
733 935
734 936 global $taboolaWP;
735 937 $taboolaWP = new TaboolaWP();
736 938
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'.
745 939 function tb_write_log($log_msg)
746 940 {
941 + if (!TABOOLA_DEBUG_MODE)
942 + return;
943 +
747 944 $log_filename = "logs";
748 945 if (!file_exists($log_filename))
749 946 {
750 947 mkdir($log_filename, 0777, true);
@@ -750,18 +947,14 @@
750 947 mkdir($log_filename, 0777, true);
751 948 }
752 949 $log_file_data = $log_filename.'/debug.log';
753 950
754 - // Add date/time
755 951 $date = date('Y-m-d H:i:s');
756 952 $log_msg_with_date = $date." : ".$log_msg;
757 -
758 - if (TABOOLA_DEBUG_MODE)
759 - file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
760 -
953 +
954 + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
761 955 }
762 956
763 -// Write to console
764 957 function tb_console_log( $data ){
765 958
766 959 If(wp_get_environment_type() === 'development') {
767 960
@@ -771,9 +964,8 @@
771 964
772 965 }
773 966 }
774 967
775 -// Write to webpage and stop
776 968 function tb_print_to_page($data) {
777 969 If(wp_get_environment_type() === 'development') {
778 970 print_r($data);
779 971 die;
@@ -779,5 +971,16 @@
779 971 die;
780 972 }
781 973 }
782 974
783 -//
975 +function enqueue_taboola_scripts() {
976 + wp_register_script(
977 + 'taboola-injector',
978 + plugins_url('js/js_inject.min.js', __FILE__),
979 + array(),
980 + null,
981 + true
982 + );
983 +
984 + wp_enqueue_script('taboola-injector');
985 +}
986 +add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts');