PluginProbe
Taboola / 2.2.3
Taboola v2.2.3
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 +206 -48 2.0.12.2.3 View file →
@@ -1,18 +1,18 @@
1 1 <?php
2 2 /**
3 3 * Plugin Name: Taboola
4 - * Plugin URI: https://developers.taboola.com/web-integrations/discuss
4 + * Plugin URI: https://developers.taboola.com/web-integrations/docs/wordpress-plugin
5 5 * Description: Taboola
6 - * Version: 2.0.1
6 + * Version: 2.2.3
7 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","2.2.3"); // => UPDATE THIS FOR *EVERY* RELEASE (USED FOR TRACKING)
11 +define ("TABOOLA_MIN_VER","2.2.2"); // => UPDATE THIS *ONLY* IF THIS RELEASE HAS *DB CHANGES*
12 +define ("TABOOLA_DEBUG_MODE", false); // => SET THIS TO 'FALSE' FOR *EVERY* RELEASE (USED TO SUPRESS DEBUGGING LOGS)
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"); // Note: if this release has DB changes, then the min version will be saved under 'taboola_plugin_version' in 'wp_options'.
15 15
16 16 define ("TABOOLA_XPATH_MARKER","/");
17 17 define ("TABOOLA_JS_INDICATOR","{JS}");
18 18 define ("TABOOLA_JS_MARKER","{");
@@ -23,8 +23,9 @@
23 23
24 24 include_once('widget.php');
25 25 require_once('JavaScriptWrapper.php');
26 26
27 +
27 28 if (!class_exists('TaboolaWP')) {
28 29 class TaboolaWP
29 30 {
30 31 //save internal data
@@ -30,10 +31,28 @@
30 31 //save internal data
31 32 public $data = array();
32 33 public $_is_widget_on_page;
33 34 public $_is_head_script_loaded = false;
35 + private $tpl_sw = 'importScripts("https://cdn.taboola.com/webpush/tsw.js");';
34 36
35 - function __construct()
37 + private $msg_sw_error = <<<SWE
38 + <p>
39 + The file sw.js in the root directory of Wordpress is not writable.
40 + Please change its permissions and try again. Otherwise replace its contents manually:
41 + </p>
42 + <pre><code>{{SW}}</code></pre>
43 + <p>
44 + Also make sure that the file is accessible at {{DOMAIN}}/sw.js
45 + </p>
46 + SWE;
47 +
48 + public $plugin_name;
49 + public $plugin_directory;
50 + public $plugin_url;
51 + public $settings;
52 + public $tbl_taboola_settings;
53 +
54 + public function __construct()
36 55 {
37 56 global $wpdb;
38 57
39 58 //initialize plugin constant
@@ -68,17 +87,75 @@
68 87 if (is_admin()) {
69 88 //add menu for plugin
70 89 add_action( 'admin_menu', array(&$this, 'admin_generate_menu') );
71 90 add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
72 - }else if ($this->settings != NULL){
91 + }elseif ($this->settings != NULL){
73 92 add_action('wp_head', array(&$this, 'taboola_header_loader_inject'));
93 + if (!empty($this->settings->publisher_id_push)) {
94 + add_action('wp_head', array(&$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 + }
110 + }
74 111 add_action('wp_footer', array(&$this, 'taboola_footer_loader_js'));
75 112 add_filter('the_content', array(&$this, 'load_taboola_content'));
76 113 add_filter('the_content', array(&$this, 'load_taboola_content_mid'));
77 - add_filter('the_content', array(&$this, 'load_taboola_content_home'));
78 - }
114 + //add_filter('the_content', array(&$this, 'load_taboola_content_home'));
115 + // add_filter('the_excerpt', array(&$this, 'load_taboola_content_home'));
116 + // Homepage widget – capture the full page, inject once.
117 + // Homepage widget – start buffering *before* anything else renders
118 + add_action( 'template_redirect', [ $this, 'tb_home_buffer_start' ], 0 );
119 +
120 + // Homepage widget – flush the buffer *after* literally everything else
121 + add_action( 'shutdown', [ $this, 'tb_home_buffer_flush' ], PHP_INT_MAX );
122 +
123 +
124 + }
125 +
79 126 }
127 +
128 + /* ------------------------------------------------------------------
129 + * HOMEPAGE WIDGET – full-page buffer
130 + * ----------------------------------------------------------------*/
131 +public function tb_home_buffer_start() {
132 +
133 + // Use the plug-in’s own logic to decide if the home widget is enabled
134 + if ( $this->should_show_content_widget_home() ) {
135 + ob_start( [ $this, 'tb_home_buffer_inject' ] );
136 + }
137 +}
80 138
139 +public function tb_home_buffer_inject( $html ) {
140 + //error_log( '🟢 Taboola buffer inject ran. HTML length = ' . strlen( $html ) ); // LOG #1
141 + // error_log( 'TB-DEBUG â‘  buffer inject, html len='.strlen($html) );
142 +
143 + /*
144 + * load_taboola_content_home() already:
145 + * – reads your saved widget-ID, placement, selector & occurrence
146 + * – builds the HTML + JS
147 + * – calls embed_taboola_content_location_home() to place it
148 + */
149 + return $this->load_taboola_content_home( $html );
150 +}
151 +
152 +public function tb_home_buffer_flush() {
153 + if ( ob_get_length() ) {
154 + echo ob_get_clean();
155 + }
156 +}
157 +
81 158 function plugin_action_links($links, $file) {
82 159 static $this_plugin;
83 160
84 161 if (!$this_plugin) {
@@ -107,11 +184,14 @@
107 184 }
108 185
109 186 private function should_show_content_widget_home(){
110 187 // PC - only if v2 was installed:
188 +
189 + //error_log( print_r( $this->settings, true ) ); // TEMP line
190 +
111 191 if (!$this->is_db_updated_for_min_ver("2.0.0"))
112 192 return false;
113 - $retVal2 = ((trim($this->settings->publisher_id) != '') && is_front_page() && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
193 + $retVal2 = ((trim($this->settings->publisher_id) != '') && (is_front_page() || is_home()) && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
114 194 return $retVal2;
115 195 }
116 196
117 197 private function should_show_sidebar_widget(){
@@ -139,9 +219,10 @@
139 219 $head_string = "";
140 220
141 221 // Only adding the loader if a widget is going to be placed on the page.
142 222 if ($this->is_widget_on_page()){
143 -
223 + // PC - since these params will be inserted in 'loaderInjectionScript.js' via search and replace,
224 + // double brackets are used to ensure that each key is a unique string.
144 225 $stringParams = array(
145 226 '{{PUBLISHER_ID}}' => $this->settings->publisher_id,
146 227 '{{PAGE_TYPE}}' => $this->get_page_type(),
147 228 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
@@ -153,9 +234,8 @@
153 234
154 235 $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
155 236 $head_string = $scriptWrapper->getScriptMarkupString();
156 237 }
157 -
158 238 return $head_string;
159 239 }
160 240
161 241
@@ -163,13 +243,23 @@
163 243 function taboola_header_loader_inject(){
164 244 echo $this->taboola_header_loader_js();
165 245 }
166 246
247 + // adding webpush loader
248 + function taboola_webpush_loader_js() {
249 + if(is_single() || is_home() || is_category() || is_front_page()){
250 + $stringParamsWeb = array(
251 + '{{PUBLISHER_ID}}' => $this->settings->publisher_id_push
252 + );
253 + $webpushInjectionScript = new JavaScriptWrapper("webPush.js",$stringParamsWeb);
254 + echo $webpushInjectionScript;
255 + }
256 + }
167 257
168 258 function taboola_footer_loader_js() {
169 259
170 260 // Only adding flush script if a widget is going to be placed on the page.
171 - if ( $this->is_widget_on_page() ){
261 + if ($this->is_widget_on_page()){
172 262 $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array());
173 263 echo $flushInjectionScript->getScriptMarkupString();
174 264 }
175 265 }
@@ -219,9 +309,9 @@
219 309
220 310 // Homepage widget
221 311 function load_taboola_content_home($content)
222 312 {
223 -
313 + //error_log( 'TB-DEBUG â‘¡ entered load_taboola_content_home' );
224 314 $taboola_content_home = array();
225 315 if ($this->should_show_content_widget_home()){
226 316
227 317 $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id,
@@ -385,8 +475,22 @@
385 475 // Homepage widget
386 476 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
387 477 function embed_taboola_content_location_home($content, $taboola_content_home, $location){
388 478
479 +
480 + // error_log( 'TB-DEBUG â‘¢ raw selector string: [' . $location . ']' );
481 +
482 + // load parser
483 + //require_once plugin_dir_path( __FILE__ ) . 'simple_html_dom.php';
484 + // $html_doc = str_get_html( $content );
485 +
486 + // occurrence: settings is 1-based, find() wants 0-based
487 + // $occurrence = max( 1, intval( $this->settings->home_location_string_occurrence ) );
488 + // $node = $html_doc->find( $location, $occurrence - 1 );
489 + // $found = is_object( $node ) ? 1 : 0;
490 +
491 + // error_log( "TB-DEBUG â‘¢ found {$found} matching node(s) for selector [{$location}]" );
492 + // ────────────────────────────
389 493 $do_default = true;
390 494
391 495 if (isset($location) && $location != ''){
392 496 $first_char = substr($location,0,1);
@@ -426,18 +530,19 @@
426 530 }
427 531 }
428 532 }
429 533 // 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 - // }
534 + if ($do_default){
535 + $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
536 + }
433 537 return $content;
538 +
434 539
435 540 }
436 541
437 542 function admin_generate_menu(){
438 543 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);
544 + 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 545 }
441 546
442 547 function admin_taboola_settings(){
443 548 global $wpdb;
@@ -444,29 +549,35 @@
444 549 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
445 550 $taboola_errors = array();
446 551 if($_SERVER['REQUEST_METHOD'] == 'POST'){
447 552
448 - if(trim($_POST['publisher_id']) == ''){
553 + if(trim(strip_tags($_POST['publisher_id'])) == ''){
449 554 $taboola_errors[] = "Publisher ID";
450 555 }
451 556
557 + if(isset($_POST['web_push_enabled'])) {
558 + if (trim(strip_tags($_POST['publisher_id_push'])) == '') {
559 + $taboola_errors[] = "Publisher Push ID";
560 + }
561 + }
562 +
452 563 if(isset($_POST['first_bc_enabled'])) {
453 - if (trim($_POST['first_bc_widget_id']) == '') {
564 + if (trim(strip_tags($_POST['first_bc_widget_id'])) == '') {
454 565 $taboola_errors[] = "Below-article > Widget ID";
455 566 }
456 - if (trim($_POST['first_bc_placement']) == '') {
567 + if (trim(strip_tags($_POST['first_bc_placement'])) == '') {
457 568 $taboola_errors[] = "Below-article > Placement Name";
458 569 }
459 570 }
460 571
461 572 if(isset($_POST['mid_enabled'])) {
462 - if (trim($_POST['mid_widget_id']) == '') {
573 + if (trim(strip_tags($_POST['mid_widget_id'])) == '') {
463 574 $taboola_errors[] = "Mid-article > Widget ID";
464 575 }
465 - if (trim($_POST['mid_placement']) == '') {
576 + if (trim(strip_tags($_POST['mid_placement'])) == '') {
466 577 $taboola_errors[] = "Mid-article > Placement Name";
467 578 }
468 - if (trim($_POST['mid_location_string']) == '') {
579 + if (trim(strip_tags($_POST['mid_location_string'])) == '') {
469 580 $taboola_errors[] = "Mid-article > CSS selector";
470 581 }
471 582 else {
472 583 // If the 'location' WAS filled in, then...
@@ -486,16 +597,16 @@
486 597
487 598 }
488 599
489 600 if(isset($_POST['home_enabled'])) {
490 - if (trim($_POST['home_widget_id']) == '') {
601 + if (trim(strip_tags($_POST['home_widget_id'])) == '') {
491 602 $taboola_errors[] = "Homepage > Widget ID";
492 603 }
493 - if (trim($_POST['home_placement']) == '') {
604 + if (trim(strip_tags($_POST['home_placement'])) == '') {
494 605 $taboola_errors[] = "Homepage > Placement Name";
495 606 }
496 607
497 - if (trim($_POST['home_location_string']) == '') {
608 + if (trim(strip_tags($_POST['home_location_string'])) == '') {
498 609 $taboola_errors[] = "Homepage > CSS selector";
499 610 }
500 611 else {
501 612 // If the 'location' WAS filled in, then...
@@ -514,12 +625,14 @@
514 625 }
515 626 }
516 627
517 628 if(count($taboola_errors) == 0){
518 -
519 629 $data = array(
520 630 "publisher_id" => trim($_POST['publisher_id']),
521 631
632 + "web_push_enabled" => isset($_POST['web_push_enabled']) ? true : false,
633 + "publisher_id_push" => !empty($_POST['publisher_id_push']) ? trim($_POST['publisher_id_push']) : '',
634 +
522 635 "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false,
523 636 "first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '',
524 637 "first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '',
525 638
@@ -541,20 +654,28 @@
541 654 "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : ''
542 655 );
543 656
544 657 //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));
658 +
659 + $is_valid_nonce = false;
660 +
661 + if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) {
662 + $is_valid_nonce = true;
549 663 }
664 +
665 + if ($is_valid_nonce) {
666 + if($settings == NULL){
667 + $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
668 + } else {
669 + $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
670 + }
671 + }
550 672 }
551 673 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
552 674 }
553 -
554 675 include_once('settings.php');
555 676 }
556 -
677 +
557 678 function is_mid_location_string_valid($mid_location_string){
558 679 // TODO:: validate the location string
559 680 return true;
560 681 }
@@ -578,8 +699,9 @@
578 699 return true;
579 700 }
580 701 return false;
581 702 }
703 +
582 704 function columnExists($column_name) {
583 705 global $wpdb;
584 706 $table_name = $wpdb->prefix . "_taboola_settings";
585 707 tb_write_log("table name : " . $table_name);
@@ -605,9 +727,8 @@
605 727 else {
606 728 tb_write_log("This is NOT a v1 upgrade!"); //PC
607 729 return false;
608 730 }
609 -
610 731 }
611 732
612 733 function is_db_updated_for_min_ver($min_ver) {
613 734 /**
@@ -613,10 +734,28 @@
613 734 /**
614 735 * Checks if the DB was *previously* updated for the minimum version specified.
615 736 * $min_ver should be passed in a format like this: "2.1.0"
616 737 */
738 +
617 739 global $wpdb;
618 740
741 + // PC - Temporary patch for v2.1.0
742 +
743 + // START patch ===============================================
744 + // Check if the `_taboola` table exists.
745 + // If not, then return false.
746 +
747 + $table_name = $wpdb->prefix . "_taboola_settings";
748 +
749 + if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
750 + tb_write_log("Table NOT FOUND");
751 + return false;
752 + }
753 + else {
754 + tb_write_log("Table EXISTS");
755 + }
756 + // END patch =================================================
757 +
619 758 // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
620 759 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
621 760
622 761 $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
@@ -626,8 +765,9 @@
626 765 }
627 766
628 767 function is_db_updated_for_current_ver() {
629 768 /**
769 + * DEPRECATED
630 770 * Checks if the DB was *previously* updated for the version currently loaded.
631 771 */
632 772 global $wpdb;
633 773
@@ -633,9 +773,9 @@
633 773
634 774 // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
635 775 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
636 776
637 - // Get the currently loaded plugin version from wp_options.
777 + // Get the currently loaded plugin version.
638 778 $plugin_version = $this->get_loaded_plugin_version();
639 779
640 780 $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
641 781 //tb_write_log($db_is_up_to_date);
@@ -651,14 +791,16 @@
651 791
652 792 }
653 793
654 794 function save_taboola_version($min_ver) {
655 - // Get the currently loaded plugin version
656 - // $plugin_version = $this->get_loaded_plugin_version();
795 + /**
796 + * Checks for the saved version in wp_options.
797 + * If not found - adds it. Else - updates it.
798 + */
799 +
657 800
658 801 tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC
659 802
660 - // Check the saved version in wp_options. Default to '' if the option is not found.
661 803 $saved_version = get_option(TABOOLA_OPTION_NAME, '');
662 804 if ($saved_version == '') {
663 805 add_option(TABOOLA_OPTION_NAME, $min_ver);
664 806 }
@@ -667,12 +809,12 @@
667 809 }
668 810 }
669 811
670 812 function update_db(){
671 -
813 +
672 814 // If we are up to date, then skip this method...
673 815 if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
674 - //tb_write_log("All up to date!");
816 + //tb_write_log("All up to date!");
675 817 return;
676 818 }
677 819
678 820 $this->save_taboola_version(TABOOLA_MIN_VER);
@@ -680,9 +822,9 @@
680 822 global $wpdb;
681 823 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
682 824
683 825
684 - //check mysql version
826 + // Handling for older MySQL versions
685 827 if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
686 828 if (!empty($wpdb->charset))
687 829 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
688 830 if (!empty($wpdb->collate))
@@ -699,8 +841,10 @@
699 841 $sql_table_settings = "
700 842 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
701 843 `id` INT NOT NULL AUTO_INCREMENT ,
702 844 `publisher_id` VARCHAR(255) DEFAULT NULL,
845 + `web_push_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
846 + `publisher_id_push` INT(15) DEFAULT NULL,
703 847 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
704 848 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
705 849 `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .",
706 850 `first_bc_custom_css` TEXT DEFAULT NULL,
@@ -743,8 +887,11 @@
743 887 // In this script, we trigger the function from the admin dashboard.
744 888 // So the log file is located under 'wp-admin/logs'.
745 889 function tb_write_log($log_msg)
746 890 {
891 + if (!TABOOLA_DEBUG_MODE) // If not debug mode, do nothing
892 + return;
893 +
747 894 $log_filename = "logs";
748 895 if (!file_exists($log_filename))
749 896 {
750 897 mkdir($log_filename, 0777, true);
@@ -753,12 +900,10 @@
753 900
754 901 // Add date/time
755 902 $date = date('Y-m-d H:i:s');
756 903 $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 -
904 +
905 + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
761 906 }
762 907
763 908 // Write to console
764 909 function tb_console_log( $data ){
@@ -778,6 +923,19 @@
778 923 print_r($data);
779 924 die;
780 925 }
781 926 }
927 +//
782 928
783 -//
929 +// Enqueue js_inject.min.js script for XPath injection
930 +function enqueue_taboola_scripts() {
931 + wp_register_script(
932 + 'taboola-injector',
933 + plugins_url('js/js_inject.min.js', __FILE__),
934 + array(), // No dependencies
935 + null, // No version specified
936 + true // Load in footer
937 + );
938 +
939 + wp_enqueue_script('taboola-injector');
940 +}
941 +add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts');