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 +190 -45 2.0.22.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.2
6 + * Version: 2.2.3
7 7 * Author: Taboola
8 8 */
9 9
10 -define ("TABOOLA_PLUGIN_VERSION","2.0.2"); // => 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","{");
@@ -31,10 +31,28 @@
31 31 //save internal data
32 32 public $data = array();
33 33 public $_is_widget_on_page;
34 34 public $_is_head_script_loaded = false;
35 + private $tpl_sw = 'importScripts("https://cdn.taboola.com/webpush/tsw.js");';
35 36
36 - 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()
37 55 {
38 56 global $wpdb;
39 57
40 58 //initialize plugin constant
@@ -69,17 +87,75 @@
69 87 if (is_admin()) {
70 88 //add menu for plugin
71 89 add_action( 'admin_menu', array(&$this, 'admin_generate_menu') );
72 90 add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
73 - }else if ($this->settings != NULL){
91 + }elseif ($this->settings != NULL){
74 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 + }
75 111 add_action('wp_footer', array(&$this, 'taboola_footer_loader_js'));
76 112 add_filter('the_content', array(&$this, 'load_taboola_content'));
77 113 add_filter('the_content', array(&$this, 'load_taboola_content_mid'));
78 - add_filter('the_content', array(&$this, 'load_taboola_content_home'));
79 - }
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 +
80 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 +}
81 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 +
82 158 function plugin_action_links($links, $file) {
83 159 static $this_plugin;
84 160
85 161 if (!$this_plugin) {
@@ -108,11 +184,14 @@
108 184 }
109 185
110 186 private function should_show_content_widget_home(){
111 187 // PC - only if v2 was installed:
188 +
189 + //error_log( print_r( $this->settings, true ) ); // TEMP line
190 +
112 191 if (!$this->is_db_updated_for_min_ver("2.0.0"))
113 192 return false;
114 - $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) != '');
115 194 return $retVal2;
116 195 }
117 196
118 197 private function should_show_sidebar_widget(){
@@ -141,9 +220,9 @@
141 220
142 221 // Only adding the loader if a widget is going to be placed on the page.
143 222 if ($this->is_widget_on_page()){
144 223 // PC - since these params will be inserted in 'loaderInjectionScript.js' via search and replace,
145 - // double brackets are used to ensure that each key is a unique string.
224 + // double brackets are used to ensure that each key is a unique string.
146 225 $stringParams = array(
147 226 '{{PUBLISHER_ID}}' => $this->settings->publisher_id,
148 227 '{{PAGE_TYPE}}' => $this->get_page_type(),
149 228 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
@@ -155,9 +234,8 @@
155 234
156 235 $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
157 236 $head_string = $scriptWrapper->getScriptMarkupString();
158 237 }
159 -
160 238 return $head_string;
161 239 }
162 240
163 241
@@ -165,13 +243,23 @@
165 243 function taboola_header_loader_inject(){
166 244 echo $this->taboola_header_loader_js();
167 245 }
168 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 + }
169 257
170 258 function taboola_footer_loader_js() {
171 259
172 260 // Only adding flush script if a widget is going to be placed on the page.
173 - if ( $this->is_widget_on_page() ){
261 + if ($this->is_widget_on_page()){
174 262 $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array());
175 263 echo $flushInjectionScript->getScriptMarkupString();
176 264 }
177 265 }
@@ -221,9 +309,9 @@
221 309
222 310 // Homepage widget
223 311 function load_taboola_content_home($content)
224 312 {
225 -
313 + //error_log( 'TB-DEBUG â‘¡ entered load_taboola_content_home' );
226 314 $taboola_content_home = array();
227 315 if ($this->should_show_content_widget_home()){
228 316
229 317 $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id,
@@ -387,8 +475,22 @@
387 475 // Homepage widget
388 476 // Do the actual logic of choosing where to place the taboola content based on the "location" attribute
389 477 function embed_taboola_content_location_home($content, $taboola_content_home, $location){
390 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 + // ────────────────────────────
391 493 $do_default = true;
392 494
393 495 if (isset($location) && $location != ''){
394 496 $first_char = substr($location,0,1);
@@ -428,48 +530,54 @@
428 530 }
429 531 }
430 532 }
431 533 // Default for below-article widget - add to the end of the content
432 - // if ($do_default){
433 - // $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
434 - // }
534 + if ($do_default){
535 + $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING);
536 + }
435 537 return $content;
538 +
436 539
437 540 }
438 541
439 542 function admin_generate_menu(){
440 543 global $current_user;
441 - add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110);
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);
442 545 }
443 546
444 547 function admin_taboola_settings(){
445 -
446 548 global $wpdb;
447 549 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
448 550 $taboola_errors = array();
449 551 if($_SERVER['REQUEST_METHOD'] == 'POST'){
450 552
451 - if(trim($_POST['publisher_id']) == ''){
553 + if(trim(strip_tags($_POST['publisher_id'])) == ''){
452 554 $taboola_errors[] = "Publisher ID";
453 555 }
454 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 +
455 563 if(isset($_POST['first_bc_enabled'])) {
456 - if (trim($_POST['first_bc_widget_id']) == '') {
564 + if (trim(strip_tags($_POST['first_bc_widget_id'])) == '') {
457 565 $taboola_errors[] = "Below-article > Widget ID";
458 566 }
459 - if (trim($_POST['first_bc_placement']) == '') {
567 + if (trim(strip_tags($_POST['first_bc_placement'])) == '') {
460 568 $taboola_errors[] = "Below-article > Placement Name";
461 569 }
462 570 }
463 571
464 572 if(isset($_POST['mid_enabled'])) {
465 - if (trim($_POST['mid_widget_id']) == '') {
573 + if (trim(strip_tags($_POST['mid_widget_id'])) == '') {
466 574 $taboola_errors[] = "Mid-article > Widget ID";
467 575 }
468 - if (trim($_POST['mid_placement']) == '') {
576 + if (trim(strip_tags($_POST['mid_placement'])) == '') {
469 577 $taboola_errors[] = "Mid-article > Placement Name";
470 578 }
471 - if (trim($_POST['mid_location_string']) == '') {
579 + if (trim(strip_tags($_POST['mid_location_string'])) == '') {
472 580 $taboola_errors[] = "Mid-article > CSS selector";
473 581 }
474 582 else {
475 583 // If the 'location' WAS filled in, then...
@@ -489,16 +597,16 @@
489 597
490 598 }
491 599
492 600 if(isset($_POST['home_enabled'])) {
493 - if (trim($_POST['home_widget_id']) == '') {
601 + if (trim(strip_tags($_POST['home_widget_id'])) == '') {
494 602 $taboola_errors[] = "Homepage > Widget ID";
495 603 }
496 - if (trim($_POST['home_placement']) == '') {
604 + if (trim(strip_tags($_POST['home_placement'])) == '') {
497 605 $taboola_errors[] = "Homepage > Placement Name";
498 606 }
499 607
500 - if (trim($_POST['home_location_string']) == '') {
608 + if (trim(strip_tags($_POST['home_location_string'])) == '') {
501 609 $taboola_errors[] = "Homepage > CSS selector";
502 610 }
503 611 else {
504 612 // If the 'location' WAS filled in, then...
@@ -517,12 +625,14 @@
517 625 }
518 626 }
519 627
520 628 if(count($taboola_errors) == 0){
521 -
522 629 $data = array(
523 630 "publisher_id" => trim($_POST['publisher_id']),
524 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 +
525 635 "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false,
526 636 "first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '',
527 637 "first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '',
528 638
@@ -558,16 +668,14 @@
558 668 } else {
559 669 $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
560 670 }
561 671 }
562 -
563 672 }
564 673 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
565 674 }
566 -
567 675 include_once('settings.php');
568 676 }
569 -
677 +
570 678 function is_mid_location_string_valid($mid_location_string){
571 679 // TODO:: validate the location string
572 680 return true;
573 681 }
@@ -591,8 +699,9 @@
591 699 return true;
592 700 }
593 701 return false;
594 702 }
703 +
595 704 function columnExists($column_name) {
596 705 global $wpdb;
597 706 $table_name = $wpdb->prefix . "_taboola_settings";
598 707 tb_write_log("table name : " . $table_name);
@@ -618,9 +727,8 @@
618 727 else {
619 728 tb_write_log("This is NOT a v1 upgrade!"); //PC
620 729 return false;
621 730 }
622 -
623 731 }
624 732
625 733 function is_db_updated_for_min_ver($min_ver) {
626 734 /**
@@ -626,10 +734,28 @@
626 734 /**
627 735 * Checks if the DB was *previously* updated for the minimum version specified.
628 736 * $min_ver should be passed in a format like this: "2.1.0"
629 737 */
738 +
630 739 global $wpdb;
631 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 +
632 758 // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
633 759 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
634 760
635 761 $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
@@ -639,8 +765,9 @@
639 765 }
640 766
641 767 function is_db_updated_for_current_ver() {
642 768 /**
769 + * DEPRECATED
643 770 * Checks if the DB was *previously* updated for the version currently loaded.
644 771 */
645 772 global $wpdb;
646 773
@@ -646,9 +773,9 @@
646 773
647 774 // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
648 775 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
649 776
650 - // Get the currently loaded plugin version from wp_options.
777 + // Get the currently loaded plugin version.
651 778 $plugin_version = $this->get_loaded_plugin_version();
652 779
653 780 $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
654 781 //tb_write_log($db_is_up_to_date);
@@ -664,14 +791,16 @@
664 791
665 792 }
666 793
667 794 function save_taboola_version($min_ver) {
668 - // Get the currently loaded plugin version
669 - // $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 +
670 800
671 801 tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC
672 802
673 - // Check the saved version in wp_options. Default to '' if the option is not found.
674 803 $saved_version = get_option(TABOOLA_OPTION_NAME, '');
675 804 if ($saved_version == '') {
676 805 add_option(TABOOLA_OPTION_NAME, $min_ver);
677 806 }
@@ -683,9 +812,9 @@
683 812 function update_db(){
684 813
685 814 // If we are up to date, then skip this method...
686 815 if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
687 - //tb_write_log("All up to date!");
816 + //tb_write_log("All up to date!");
688 817 return;
689 818 }
690 819
691 820 $this->save_taboola_version(TABOOLA_MIN_VER);
@@ -693,9 +822,9 @@
693 822 global $wpdb;
694 823 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
695 824
696 825
697 - //check mysql version
826 + // Handling for older MySQL versions
698 827 if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
699 828 if (!empty($wpdb->charset))
700 829 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
701 830 if (!empty($wpdb->collate))
@@ -712,8 +841,10 @@
712 841 $sql_table_settings = "
713 842 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
714 843 `id` INT NOT NULL AUTO_INCREMENT ,
715 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,
716 847 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
717 848 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
718 849 `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .",
719 850 `first_bc_custom_css` TEXT DEFAULT NULL,
@@ -756,8 +887,11 @@
756 887 // In this script, we trigger the function from the admin dashboard.
757 888 // So the log file is located under 'wp-admin/logs'.
758 889 function tb_write_log($log_msg)
759 890 {
891 + if (!TABOOLA_DEBUG_MODE) // If not debug mode, do nothing
892 + return;
893 +
760 894 $log_filename = "logs";
761 895 if (!file_exists($log_filename))
762 896 {
763 897 mkdir($log_filename, 0777, true);
@@ -766,12 +900,10 @@
766 900
767 901 // Add date/time
768 902 $date = date('Y-m-d H:i:s');
769 903 $log_msg_with_date = $date." : ".$log_msg;
770 -
771 - if (TABOOLA_DEBUG_MODE)
772 - file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
773 -
904 +
905 + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND);
774 906 }
775 907
776 908 // Write to console
777 909 function tb_console_log( $data ){
@@ -791,6 +923,19 @@
791 923 print_r($data);
792 924 die;
793 925 }
794 926 }
927 +//
795 928
796 -//
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');