PluginProbe
Taboola / 2.2.2
Taboola v2.2.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 +108 -35 2.0.22.2.2 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.2
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.2"); // => 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,15 +87,33 @@
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 114 add_filter('the_content', array(&$this, 'load_taboola_content_home'));
79 - }
115 + }
80 116 }
81 117
82 118 function plugin_action_links($links, $file) {
83 119 static $this_plugin;
@@ -110,9 +146,9 @@
110 146 private function should_show_content_widget_home(){
111 147 // PC - only if v2 was installed:
112 148 if (!$this->is_db_updated_for_min_ver("2.0.0"))
113 149 return false;
114 - $retVal2 = ((trim($this->settings->publisher_id) != '') && is_front_page() && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
150 + $retVal2 = ((trim($this->settings->publisher_id) != '') && (is_front_page() || is_home()) && $this->settings->home_enabled && trim($this->settings->home_widget_id) != '');
115 151 return $retVal2;
116 152 }
117 153
118 154 private function should_show_sidebar_widget(){
@@ -141,9 +177,9 @@
141 177
142 178 // Only adding the loader if a widget is going to be placed on the page.
143 179 if ($this->is_widget_on_page()){
144 180 // 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.
181 + // double brackets are used to ensure that each key is a unique string.
146 182 $stringParams = array(
147 183 '{{PUBLISHER_ID}}' => $this->settings->publisher_id,
148 184 '{{PAGE_TYPE}}' => $this->get_page_type(),
149 185 '{{WORDPRESS_VERSION}}' => get_bloginfo('version'),
@@ -155,9 +191,8 @@
155 191
156 192 $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams);
157 193 $head_string = $scriptWrapper->getScriptMarkupString();
158 194 }
159 -
160 195 return $head_string;
161 196 }
162 197
163 198
@@ -165,13 +200,23 @@
165 200 function taboola_header_loader_inject(){
166 201 echo $this->taboola_header_loader_js();
167 202 }
168 203
204 + // adding webpush loader
205 + function taboola_webpush_loader_js() {
206 + if(is_single() || is_home() || is_category() || is_front_page()){
207 + $stringParamsWeb = array(
208 + '{{PUBLISHER_ID}}' => $this->settings->publisher_id_push
209 + );
210 + $webpushInjectionScript = new JavaScriptWrapper("webPush.js",$stringParamsWeb);
211 + echo $webpushInjectionScript;
212 + }
213 + }
169 214
170 215 function taboola_footer_loader_js() {
171 216
172 217 // Only adding flush script if a widget is going to be placed on the page.
173 - if ( $this->is_widget_on_page() ){
218 + if ($this->is_widget_on_page()){
174 219 $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array());
175 220 echo $flushInjectionScript->getScriptMarkupString();
176 221 }
177 222 }
@@ -437,39 +482,44 @@
437 482 }
438 483
439 484 function admin_generate_menu(){
440 485 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);
486 + 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 487 }
443 488
444 489 function admin_taboola_settings(){
445 -
446 490 global $wpdb;
447 491 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
448 492 $taboola_errors = array();
449 493 if($_SERVER['REQUEST_METHOD'] == 'POST'){
450 494
451 - if(trim($_POST['publisher_id']) == ''){
495 + if(trim(strip_tags($_POST['publisher_id'])) == ''){
452 496 $taboola_errors[] = "Publisher ID";
453 497 }
454 498
499 + if(isset($_POST['web_push_enabled'])) {
500 + if (trim(strip_tags($_POST['publisher_id_push'])) == '') {
501 + $taboola_errors[] = "Publisher Push ID";
502 + }
503 + }
504 +
455 505 if(isset($_POST['first_bc_enabled'])) {
456 - if (trim($_POST['first_bc_widget_id']) == '') {
506 + if (trim(strip_tags($_POST['first_bc_widget_id'])) == '') {
457 507 $taboola_errors[] = "Below-article > Widget ID";
458 508 }
459 - if (trim($_POST['first_bc_placement']) == '') {
509 + if (trim(strip_tags($_POST['first_bc_placement'])) == '') {
460 510 $taboola_errors[] = "Below-article > Placement Name";
461 511 }
462 512 }
463 513
464 514 if(isset($_POST['mid_enabled'])) {
465 - if (trim($_POST['mid_widget_id']) == '') {
515 + if (trim(strip_tags($_POST['mid_widget_id'])) == '') {
466 516 $taboola_errors[] = "Mid-article > Widget ID";
467 517 }
468 - if (trim($_POST['mid_placement']) == '') {
518 + if (trim(strip_tags($_POST['mid_placement'])) == '') {
469 519 $taboola_errors[] = "Mid-article > Placement Name";
470 520 }
471 - if (trim($_POST['mid_location_string']) == '') {
521 + if (trim(strip_tags($_POST['mid_location_string'])) == '') {
472 522 $taboola_errors[] = "Mid-article > CSS selector";
473 523 }
474 524 else {
475 525 // If the 'location' WAS filled in, then...
@@ -489,16 +539,16 @@
489 539
490 540 }
491 541
492 542 if(isset($_POST['home_enabled'])) {
493 - if (trim($_POST['home_widget_id']) == '') {
543 + if (trim(strip_tags($_POST['home_widget_id'])) == '') {
494 544 $taboola_errors[] = "Homepage > Widget ID";
495 545 }
496 - if (trim($_POST['home_placement']) == '') {
546 + if (trim(strip_tags($_POST['home_placement'])) == '') {
497 547 $taboola_errors[] = "Homepage > Placement Name";
498 548 }
499 549
500 - if (trim($_POST['home_location_string']) == '') {
550 + if (trim(strip_tags($_POST['home_location_string'])) == '') {
501 551 $taboola_errors[] = "Homepage > CSS selector";
502 552 }
503 553 else {
504 554 // If the 'location' WAS filled in, then...
@@ -517,12 +567,14 @@
517 567 }
518 568 }
519 569
520 570 if(count($taboola_errors) == 0){
521 -
522 571 $data = array(
523 572 "publisher_id" => trim($_POST['publisher_id']),
524 573
574 + "web_push_enabled" => isset($_POST['web_push_enabled']) ? true : false,
575 + "publisher_id_push" => !empty($_POST['publisher_id_push']) ? trim($_POST['publisher_id_push']) : '',
576 +
525 577 "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false,
526 578 "first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '',
527 579 "first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '',
528 580
@@ -558,16 +610,14 @@
558 610 } else {
559 611 $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id));
560 612 }
561 613 }
562 -
563 614 }
564 615 $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1");
565 616 }
566 -
567 617 include_once('settings.php');
568 618 }
569 -
619 +
570 620 function is_mid_location_string_valid($mid_location_string){
571 621 // TODO:: validate the location string
572 622 return true;
573 623 }
@@ -591,8 +641,9 @@
591 641 return true;
592 642 }
593 643 return false;
594 644 }
645 +
595 646 function columnExists($column_name) {
596 647 global $wpdb;
597 648 $table_name = $wpdb->prefix . "_taboola_settings";
598 649 tb_write_log("table name : " . $table_name);
@@ -618,9 +669,8 @@
618 669 else {
619 670 tb_write_log("This is NOT a v1 upgrade!"); //PC
620 671 return false;
621 672 }
622 -
623 673 }
624 674
625 675 function is_db_updated_for_min_ver($min_ver) {
626 676 /**
@@ -626,10 +676,28 @@
626 676 /**
627 677 * Checks if the DB was *previously* updated for the minimum version specified.
628 678 * $min_ver should be passed in a format like this: "2.1.0"
629 679 */
680 +
630 681 global $wpdb;
631 682
683 + // PC - Temporary patch for v2.1.0
684 +
685 + // START patch ===============================================
686 + // Check if the `_taboola` table exists.
687 + // If not, then return false.
688 +
689 + $table_name = $wpdb->prefix . "_taboola_settings";
690 +
691 + if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
692 + tb_write_log("Table NOT FOUND");
693 + return false;
694 + }
695 + else {
696 + tb_write_log("Table EXISTS");
697 + }
698 + // END patch =================================================
699 +
632 700 // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
633 701 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
634 702
635 703 $db_is_up_to_date = version_compare($saved_version, $min_ver, '>=');
@@ -639,8 +707,9 @@
639 707 }
640 708
641 709 function is_db_updated_for_current_ver() {
642 710 /**
711 + * DEPRECATED
643 712 * Checks if the DB was *previously* updated for the version currently loaded.
644 713 */
645 714 global $wpdb;
646 715
@@ -646,9 +715,9 @@
646 715
647 716 // Check the saved version in wp_options. Default to '1.0.0' if the option is not found.
648 717 $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0');
649 718
650 - // Get the currently loaded plugin version from wp_options.
719 + // Get the currently loaded plugin version.
651 720 $plugin_version = $this->get_loaded_plugin_version();
652 721
653 722 $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>=');
654 723 //tb_write_log($db_is_up_to_date);
@@ -664,14 +733,16 @@
664 733
665 734 }
666 735
667 736 function save_taboola_version($min_ver) {
668 - // Get the currently loaded plugin version
669 - // $plugin_version = $this->get_loaded_plugin_version();
737 + /**
738 + * Checks for the saved version in wp_options.
739 + * If not found - adds it. Else - updates it.
740 + */
741 +
670 742
671 743 tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC
672 744
673 - // Check the saved version in wp_options. Default to '' if the option is not found.
674 745 $saved_version = get_option(TABOOLA_OPTION_NAME, '');
675 746 if ($saved_version == '') {
676 747 add_option(TABOOLA_OPTION_NAME, $min_ver);
677 748 }
@@ -683,9 +754,9 @@
683 754 function update_db(){
684 755
685 756 // If we are up to date, then skip this method...
686 757 if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) {
687 - //tb_write_log("All up to date!");
758 + //tb_write_log("All up to date!");
688 759 return;
689 760 }
690 761
691 762 $this->save_taboola_version(TABOOLA_MIN_VER);
@@ -693,9 +764,9 @@
693 764 global $wpdb;
694 765 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
695 766
696 767
697 - //check mysql version
768 + // Handling for older MySQL versions
698 769 if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
699 770 if (!empty($wpdb->charset))
700 771 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
701 772 if (!empty($wpdb->collate))
@@ -712,8 +783,10 @@
712 783 $sql_table_settings = "
713 784 CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` (
714 785 `id` INT NOT NULL AUTO_INCREMENT ,
715 786 `publisher_id` VARCHAR(255) DEFAULT NULL,
787 + `web_push_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
788 + `publisher_id_push` INT(15) DEFAULT NULL,
716 789 `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
717 790 `first_bc_widget_id` VARCHAR(255) DEFAULT NULL,
718 791 `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .",
719 792 `first_bc_custom_css` TEXT DEFAULT NULL,