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