| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Taboola |
| 4 |
* Plugin URI: https://developers.taboola.com/web-integrations/discuss |
| 5 |
* Description: Taboola |
| 6 |
* Version: 1.0.14 |
| 7 |
* Author: Taboola |
| 8 |
*/ |
| 9 |
|
| 10 |
define ("XPATH_MARKER","/"); |
| 11 |
define ("JS_INDICATOR","{JS}"); |
| 12 |
define ("JS_MARKER","{"); |
| 13 |
define ("TABOOLA_CONTENT_FORMAT_STRING",'string'); |
| 14 |
define ("TABOOLA_CONTENT_FORMAT_SCRIPT",'script'); |
| 15 |
define ("TABOOLA_CONTENT_FORMAT_HTML",'html'); |
| 16 |
define ("TABOOLA_PLUGIN_VERSION","1.0.14"); |
| 17 |
|
| 18 |
|
| 19 |
include_once('widget.php'); |
| 20 |
require_once('JavaScriptWrapper.php'); |
| 21 |
|
| 22 |
if (!class_exists('TaboolaWP')) { |
| 23 |
class TaboolaWP |
| 24 |
{ |
| 25 |
//save internal data |
| 26 |
public $data = array(); |
| 27 |
public $_is_widget_on_page; |
| 28 |
public $_is_head_script_loaded = false; |
| 29 |
|
| 30 |
|
| 31 |
function __construct() |
| 32 |
{ |
| 33 |
global $wpdb; |
| 34 |
|
| 35 |
//initialize plugin constant |
| 36 |
DEFINE('TaboolaWP', true); |
| 37 |
|
| 38 |
$this->_is_widget_on_page = false; |
| 39 |
$this->_is_head_script_loaded = false; |
| 40 |
|
| 41 |
$this->plugin_name = plugin_basename(__FILE__); |
| 42 |
$this->plugin_directory = plugin_dir_path(__FILE__); |
| 43 |
$this->plugin_url = trailingslashit(WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__))); |
| 44 |
$this->settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 45 |
|
| 46 |
$this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings'; |
| 47 |
|
| 48 |
//activation function |
| 49 |
register_activation_hook($this->plugin_name, array(&$this, 'activate')); |
| 50 |
|
| 51 |
// Enable sidebar widgets |
| 52 |
if($this->settings != NULL && !empty($this->settings->publisher_id)){ |
| 53 |
//register Taboola widget |
| 54 |
add_action('widgets_init', |
| 55 |
function(){ |
| 56 |
return register_widget("WP_Widget_Taboola"); |
| 57 |
} |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
//$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled; |
| 62 |
|
| 63 |
if (is_admin()) { |
| 64 |
//add menu for plugin |
| 65 |
add_action( 'admin_menu', array(&$this, 'admin_generate_menu') ); |
| 66 |
add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 ); |
| 67 |
} else { |
| 68 |
if($this->settings != NULL){ |
| 69 |
add_action('wp_head', array(&$this, 'taboola_header_loader_inject')); |
| 70 |
add_action('wp_footer', array(&$this, 'taboola_footer_loader_js')); |
| 71 |
add_filter('the_content', array(&$this, 'load_taboola_content')); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
function plugin_action_links($links, $file) { |
| 80 |
static $this_plugin; |
| 81 |
|
| 82 |
if (!$this_plugin) { |
| 83 |
$this_plugin = plugin_basename(__FILE__); |
| 84 |
} |
| 85 |
|
| 86 |
if ($file == $this_plugin) { |
| 87 |
$settings_link = '<a href="' . get_bloginfo('wpurl') . '/wp-admin/options-general.php?page=taboola_widget">Settings</a>'; |
| 88 |
array_unshift($links, $settings_link); |
| 89 |
} |
| 90 |
|
| 91 |
return $links; |
| 92 |
} |
| 93 |
|
| 94 |
private function should_show_content_widget(){ |
| 95 |
$retVal = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->first_bc_enabled && trim($this->settings->first_bc_widget_id) != ''); |
| 96 |
return $retVal; |
| 97 |
} |
| 98 |
|
| 99 |
private function should_show_sidebar_widget(){ |
| 100 |
$retVal = ((trim($this->settings->publisher_id) != '') && is_active_widget( false, false, TABOOLA_WIDGET_BASE_ID, true )); |
| 101 |
return $retVal; |
| 102 |
} |
| 103 |
|
| 104 |
// Determine if a taboola widget should be added somewhere on the current page (content or sidebar) |
| 105 |
function is_widget_on_page(){ |
| 106 |
return $this->should_show_content_widget() || $this->should_show_sidebar_widget(); |
| 107 |
} |
| 108 |
|
| 109 |
function get_page_type(){ |
| 110 |
|
| 111 |
$page_type='article'; |
| 112 |
if (is_front_page()){ |
| 113 |
$page_type='home'; |
| 114 |
}else if (is_category() || is_archive() || is_search()){ |
| 115 |
$page_type='category'; |
| 116 |
} |
| 117 |
return $page_type; |
| 118 |
} |
| 119 |
|
| 120 |
// return the head loader script |
| 121 |
function taboola_header_loader_js() { |
| 122 |
$head_string = ""; |
| 123 |
|
| 124 |
// Only adding the loader if a widget is going to be placed on the page. |
| 125 |
if ($this->is_widget_on_page()){ |
| 126 |
|
| 127 |
$stringParams = array( |
| 128 |
'{{PUBLISHER_ID}}' => $this->settings->publisher_id, |
| 129 |
'{{PAGE_TYPE}}' => $this->get_page_type(), |
| 130 |
'{{WORDPRESS_VERSION}}' => get_bloginfo('version'), |
| 131 |
'{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION, |
| 132 |
'{{LOCATION}}' => $this->settings->location_string |
| 133 |
); |
| 134 |
|
| 135 |
$scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams); |
| 136 |
$head_string = $scriptWrapper->getScripMarkupString(); |
| 137 |
} |
| 138 |
|
| 139 |
return $head_string; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
// This function is used for the hook action, injects the header content to the <head> tag. |
| 144 |
function taboola_header_loader_inject(){ |
| 145 |
|
| 146 |
echo $this->taboola_header_loader_js(); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
function taboola_footer_loader_js() { |
| 151 |
|
| 152 |
// Only adding flush script if a widget is going to be placed on the page. |
| 153 |
if ( $this->is_widget_on_page() ){ |
| 154 |
$flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array()); |
| 155 |
echo $flushInjectionScript->getScripMarkupString(); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
function load_taboola_content($content) |
| 160 |
{ |
| 161 |
$taboola_content = array(); |
| 162 |
if ($this->should_show_content_widget()){ |
| 163 |
|
| 164 |
$firstWidgetParams = array('{{WIDGET_ID}}' => $this->settings->first_bc_widget_id, |
| 165 |
'{{CONTAINER}}' => 'taboola-below-article', |
| 166 |
'{{PLACEMENT}}' => 'below-article'); |
| 167 |
|
| 168 |
$firstWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$firstWidgetParams); |
| 169 |
$taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article'></div>"; |
| 170 |
$taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $firstWidgetScript; |
| 171 |
|
| 172 |
// Adding the 2nd widget if needed |
| 173 |
if($this->settings->second_bc_enabled && trim($this->settings->second_bc_widget_id) != ''){ |
| 174 |
|
| 175 |
$secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->second_bc_widget_id, |
| 176 |
'{{CONTAINER}}' => 'taboola-below-article-second', |
| 177 |
'{{PLACEMENT}}' => 'below-article-2nd'); |
| 178 |
$secondWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$secondWidgetParams); |
| 179 |
|
| 180 |
$taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article-second'></div>"; |
| 181 |
$taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $secondWidgetScript; |
| 182 |
} |
| 183 |
|
| 184 |
$content = $this->embed_taboola_content_location($content,$taboola_content,trim($this->settings->location_string)); |
| 185 |
} |
| 186 |
|
| 187 |
return $content; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
// Extract the taboola content in the required format: |
| 192 |
// String - for injecting on the servr side |
| 193 |
// Script or HTML - for injecting on the client side |
| 194 |
function format_taboola_content($taboola_content,$format){ |
| 195 |
$ret_val = null; |
| 196 |
|
| 197 |
switch($format){ |
| 198 |
case TABOOLA_CONTENT_FORMAT_STRING: |
| 199 |
$result_string = join("",$taboola_content[TABOOLA_CONTENT_FORMAT_HTML]). |
| 200 |
"<script type='text/javascript'>".join("\n",$taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>"; |
| 201 |
$ret_val = $result_string; |
| 202 |
break; |
| 203 |
|
| 204 |
// script or html |
| 205 |
default: |
| 206 |
$ret_val = str_replace("\n","",join("",$taboola_content[$format])); |
| 207 |
break; |
| 208 |
} |
| 209 |
|
| 210 |
return $ret_val; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
// Do the actual logic of choosing where to place the taboola content based on the "location" attribute |
| 215 |
function embed_taboola_content_location($content, $taboola_content, $location){ |
| 216 |
$do_default = true; |
| 217 |
|
| 218 |
if (isset($location) && $location != ''){ |
| 219 |
$first_char = substr($location,0,1); |
| 220 |
|
| 221 |
// DIV/XPATH provided for JS handling |
| 222 |
if ($first_char == JS_MARKER){ |
| 223 |
$full_indicator = substr($location,0,strlen(JS_INDICATOR)); |
| 224 |
|
| 225 |
if ($full_indicator == JS_INDICATOR){ |
| 226 |
|
| 227 |
$xpath = substr($location,strlen(JS_INDICATOR)); |
| 228 |
$scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( |
| 229 |
"{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML), |
| 230 |
"{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT)) |
| 231 |
); |
| 232 |
$scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');"); |
| 233 |
$content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>"; |
| 234 |
|
| 235 |
$do_default = false; |
| 236 |
} |
| 237 |
|
| 238 |
// server side selector provided (see simple_html_dom selectors https://simplehtmldom.sourceforge.net/manual.htm) |
| 239 |
// basically it's CSS selectors like in jQuery |
| 240 |
} else{ |
| 241 |
|
| 242 |
require_once('simple_html_dom.php'); |
| 243 |
|
| 244 |
$html_doc = str_get_html($content); |
| 245 |
$target_location = $html_doc->find($location,0); |
| 246 |
|
| 247 |
// if the location was found within the html content |
| 248 |
if (isset($target_location) && is_object($target_location)){ |
| 249 |
|
| 250 |
// adding taboola content AFTER the target location |
| 251 |
$target_location->outertext = $target_location->outertext.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING); |
| 252 |
$content = $html_doc; |
| 253 |
$do_default = false; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
// tag is placed outside of content in order to allow "read more" functionality. |
| 258 |
}elseif ($this->settings->out_of_content_enabled){ |
| 259 |
|
| 260 |
$scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( |
| 261 |
"{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML), |
| 262 |
"{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT)) |
| 263 |
); |
| 264 |
$scriptWrapper->appendScript("injectWidgetByMarker('tbmarker');"); |
| 265 |
$content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$scriptWrapper."</script>"; |
| 266 |
$do_default = false; |
| 267 |
} |
| 268 |
|
| 269 |
// Default - add to the end of the content |
| 270 |
if ($do_default){ |
| 271 |
$content = $content.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING); |
| 272 |
} |
| 273 |
|
| 274 |
return $content; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
function admin_generate_menu(){ |
| 279 |
global $current_user; |
| 280 |
add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110); |
| 281 |
} |
| 282 |
|
| 283 |
function admin_taboola_settings(){ |
| 284 |
global $wpdb; |
| 285 |
$settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 286 |
$taboola_errors = array(); |
| 287 |
if($_SERVER['REQUEST_METHOD'] == 'POST'){ |
| 288 |
|
| 289 |
if(trim($_POST['publisher_id']) == ''){ |
| 290 |
$taboola_errors[] = "Please add a 'Publisher ID' in order to apply changes to your widgets"; |
| 291 |
} |
| 292 |
if((isset($_POST['first_bc_enabled']) && trim($_POST['first_bc_widget_id']) == '') || |
| 293 |
(isset($_POST['second_bc_enabled']) && trim($_POST['second_bc_widget_id']) == '') |
| 294 |
){ |
| 295 |
$taboola_errors[] = "Please add a 'Widget ID' in order to apply changes to your widgets"; |
| 296 |
} |
| 297 |
if (trim($_POST['location_string']) != '' && !$this->is_location_string_valid($_POST['location_string'])){ |
| 298 |
$taboola_errors[] = "Location string not valid, please contact your Taboola account manager to get a valid location string"; |
| 299 |
} |
| 300 |
if(count($taboola_errors) == 0){ |
| 301 |
|
| 302 |
$data = array( |
| 303 |
"publisher_id" => trim($_POST['publisher_id']), |
| 304 |
"first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false, |
| 305 |
"first_bc_widget_id" => trim($_POST['first_bc_widget_id']), |
| 306 |
"second_bc_enabled" => isset($_POST['second_bc_enabled']) ? true : false, |
| 307 |
"second_bc_widget_id" => trim($_POST['second_bc_widget_id']), |
| 308 |
"location_string" => trim($_POST['location_string']), |
| 309 |
"out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false |
| 310 |
); |
| 311 |
|
| 312 |
//var_dump($settings); |
| 313 |
if($settings == NULL){ |
| 314 |
$wpdb->insert($this->tbl_taboola_settings, $data, null, null); |
| 315 |
} else { |
| 316 |
$wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id)); |
| 317 |
} |
| 318 |
} |
| 319 |
$settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
include_once('settings.php'); |
| 324 |
} |
| 325 |
|
| 326 |
function is_location_string_valid($location_string){ |
| 327 |
// TODO:: validate the location string |
| 328 |
return true; |
| 329 |
} |
| 330 |
function activate(){ |
| 331 |
global $wpdb; |
| 332 |
|
| 333 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 334 |
|
| 335 |
$settings_table = $this->tbl_taboola_settings; |
| 336 |
|
| 337 |
//check mysql version |
| 338 |
if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) { |
| 339 |
if (!empty($wpdb->charset)) |
| 340 |
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; |
| 341 |
if (!empty($wpdb->collate)) |
| 342 |
$charset_collate .= " COLLATE $wpdb->collate"; |
| 343 |
} |
| 344 |
|
| 345 |
//settings table structure |
| 346 |
$sql_table_settings = " |
| 347 |
CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` ( |
| 348 |
`id` INT NOT NULL AUTO_INCREMENT , |
| 349 |
`publisher_id` VARCHAR(255) DEFAULT NULL, |
| 350 |
`first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 351 |
`first_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 352 |
`first_bc_custom_css` TEXT DEFAULT NULL, |
| 353 |
`second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 354 |
`second_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 355 |
`second_bc_custom_css` TEXT DEFAULT NULL, |
| 356 |
`location_string` TEXT DEFAULT NULL, |
| 357 |
`out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE, |
| 358 |
PRIMARY KEY (`id`) |
| 359 |
)" . $charset_collate . ";"; |
| 360 |
|
| 361 |
// create/update the table |
| 362 |
dbDelta($sql_table_settings); |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
global $taboolaWP; |
| 368 |
$taboolaWP = new TaboolaWP(); |
| 369 |
|
| 370 |
// |
| 371 |
|