| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Taboola |
| 4 |
* Plugin URI: https://developers.taboola.com/web-integrations/docs/wordpress-plugin |
| 5 |
* Description: Taboola |
| 6 |
* Version: 2.1.1 |
| 7 |
* Author: Taboola |
| 8 |
*/ |
| 9 |
|
| 10 |
define ("TABOOLA_PLUGIN_VERSION","2.1.1"); // => UPDATE THIS FOR *EVERY* RELEASE (USED FOR TRACKING) |
| 11 |
define ("TABOOLA_MIN_VER","2.0.1"); // => 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 |
|
| 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 |
|
| 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 |
|
| 23 |
|
| 24 |
include_once('widget.php'); |
| 25 |
require_once('JavaScriptWrapper.php'); |
| 26 |
|
| 27 |
|
| 28 |
if (!class_exists('TaboolaWP')) { |
| 29 |
class TaboolaWP |
| 30 |
{ |
| 31 |
//save internal data |
| 32 |
public $data = array(); |
| 33 |
public $_is_widget_on_page; |
| 34 |
public $_is_head_script_loaded = false; |
| 35 |
|
| 36 |
function __construct() |
| 37 |
{ |
| 38 |
global $wpdb; |
| 39 |
|
| 40 |
//initialize plugin constant |
| 41 |
DEFINE('TaboolaWP', true); |
| 42 |
|
| 43 |
$this->_is_widget_on_page = false; |
| 44 |
$this->_is_head_script_loaded = false; |
| 45 |
|
| 46 |
$this->plugin_name = plugin_basename(__FILE__); |
| 47 |
$this->plugin_directory = plugin_dir_path(__FILE__); |
| 48 |
$this->plugin_url = trailingslashit(WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__))); |
| 49 |
$this->settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 50 |
|
| 51 |
$this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings'; |
| 52 |
|
| 53 |
//activation function |
| 54 |
register_activation_hook($this->plugin_name, array(&$this, 'update_db')); |
| 55 |
add_action('admin_init', array(&$this, 'update_db')); |
| 56 |
|
| 57 |
// Enable sidebar widgets |
| 58 |
if ($this->settings != NULL && !empty($this->settings->publisher_id)){ |
| 59 |
//register Taboola widget |
| 60 |
add_action('widgets_init', |
| 61 |
function(){ |
| 62 |
return register_widget("WP_Widget_Taboola"); |
| 63 |
} |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
//$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled; |
| 68 |
|
| 69 |
if (is_admin()) { |
| 70 |
//add menu for plugin |
| 71 |
add_action( 'admin_menu', array(&$this, 'admin_generate_menu') ); |
| 72 |
add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 ); |
| 73 |
}else if ($this->settings != NULL){ |
| 74 |
add_action('wp_head', array(&$this, 'taboola_header_loader_inject')); |
| 75 |
add_action('wp_footer', array(&$this, 'taboola_footer_loader_js')); |
| 76 |
add_filter('the_content', array(&$this, 'load_taboola_content')); |
| 77 |
add_filter('the_content', array(&$this, 'load_taboola_content_mid')); |
| 78 |
add_filter('the_content', array(&$this, 'load_taboola_content_home')); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
function plugin_action_links($links, $file) { |
| 83 |
static $this_plugin; |
| 84 |
|
| 85 |
if (!$this_plugin) { |
| 86 |
$this_plugin = plugin_basename(__FILE__); |
| 87 |
} |
| 88 |
|
| 89 |
if ($file == $this_plugin) { |
| 90 |
$settings_link = '<a href="' . get_bloginfo('wpurl') . '/wp-admin/options-general.php?page=taboola_widget">Settings</a>'; |
| 91 |
array_unshift($links, $settings_link); |
| 92 |
} |
| 93 |
|
| 94 |
return $links; |
| 95 |
} |
| 96 |
|
| 97 |
private function should_show_content_widget(){ |
| 98 |
$retVal = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->first_bc_enabled && trim($this->settings->first_bc_widget_id) != ''); |
| 99 |
return $retVal; |
| 100 |
} |
| 101 |
|
| 102 |
private function should_show_content_widget_mid(){ |
| 103 |
// PC - only if v2 was installed: |
| 104 |
if (!$this->is_db_updated_for_min_ver("2.0.0")) |
| 105 |
return false; |
| 106 |
$retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->mid_enabled && trim($this->settings->mid_widget_id) != ''); |
| 107 |
return $retVal1; |
| 108 |
} |
| 109 |
|
| 110 |
private function should_show_content_widget_home(){ |
| 111 |
// PC - only if v2 was installed: |
| 112 |
if (!$this->is_db_updated_for_min_ver("2.0.0")) |
| 113 |
return false; |
| 114 |
$retVal2 = ((trim($this->settings->publisher_id) != '') && is_front_page() && $this->settings->home_enabled && trim($this->settings->home_widget_id) != ''); |
| 115 |
return $retVal2; |
| 116 |
} |
| 117 |
|
| 118 |
private function should_show_sidebar_widget(){ |
| 119 |
$retVal = ((trim($this->settings->publisher_id) != '') && is_active_widget( false, false, TABOOLA_WIDGET_BASE_ID, true )); |
| 120 |
return $retVal; |
| 121 |
} |
| 122 |
|
| 123 |
// Determine if a taboola widget should be added somewhere on the current page (content or sidebar) |
| 124 |
function is_widget_on_page(){ |
| 125 |
return $this->should_show_content_widget() || $this->should_show_content_widget_mid() || $this->should_show_content_widget_home() || $this->should_show_sidebar_widget(); |
| 126 |
} |
| 127 |
|
| 128 |
function get_page_type(){ |
| 129 |
$page_type='article'; |
| 130 |
if (is_front_page()){ |
| 131 |
$page_type='home'; |
| 132 |
}else if (is_category() || is_archive() || is_search()){ |
| 133 |
$page_type='category'; |
| 134 |
} |
| 135 |
return $page_type; |
| 136 |
} |
| 137 |
|
| 138 |
// return the head loader script |
| 139 |
function taboola_header_loader_js() { |
| 140 |
$head_string = ""; |
| 141 |
|
| 142 |
// Only adding the loader if a widget is going to be placed on the page. |
| 143 |
if ($this->is_widget_on_page()){ |
| 144 |
// 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. |
| 146 |
$stringParams = array( |
| 147 |
'{{PUBLISHER_ID}}' => $this->settings->publisher_id, |
| 148 |
'{{PAGE_TYPE}}' => $this->get_page_type(), |
| 149 |
'{{WORDPRESS_VERSION}}' => get_bloginfo('version'), |
| 150 |
'{{PHP_VERSION}}' => phpversion(), |
| 151 |
'{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION, |
| 152 |
'{{LOC_MID}}' => $this->settings->mid_location_string, |
| 153 |
'{{LOC_HOME}}' => $this->settings->home_location_string |
| 154 |
); |
| 155 |
|
| 156 |
$scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams); |
| 157 |
$head_string = $scriptWrapper->getScriptMarkupString(); |
| 158 |
} |
| 159 |
|
| 160 |
return $head_string; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
// This function is used for the hook action, injects the header content to the <head> tag. |
| 165 |
function taboola_header_loader_inject(){ |
| 166 |
echo $this->taboola_header_loader_js(); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
function taboola_footer_loader_js() { |
| 171 |
|
| 172 |
// Only adding flush script if a widget is going to be placed on the page. |
| 173 |
if ( $this->is_widget_on_page() ){ |
| 174 |
$flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array()); |
| 175 |
echo $flushInjectionScript->getScriptMarkupString(); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// Below-article widget |
| 180 |
function load_taboola_content($content) |
| 181 |
{ |
| 182 |
$taboola_content = array(); |
| 183 |
if ($this->should_show_content_widget()){ |
| 184 |
|
| 185 |
$firstWidgetParams = array( |
| 186 |
'{{WIDGET_ID}}' => $this->settings->first_bc_widget_id, |
| 187 |
'{{CONTAINER}}' => 'taboola-below-article-thumbnails', |
| 188 |
// PC - If v2 was not installed, then use v1 logic |
| 189 |
'{{PLACEMENT}}' => (!empty($this->settings->first_bc_placement)) ? $this->settings->first_bc_placement : 'below-article' // In case v1 upgrade has not run yet |
| 190 |
); |
| 191 |
|
| 192 |
$firstWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$firstWidgetParams); |
| 193 |
$taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article-thumbnails'></div>"; |
| 194 |
$taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $firstWidgetScript; |
| 195 |
|
| 196 |
$content = $this->embed_taboola_content_location($content,$taboola_content); |
| 197 |
} |
| 198 |
|
| 199 |
return $content; |
| 200 |
} |
| 201 |
|
| 202 |
// Mid-article-widget |
| 203 |
function load_taboola_content_mid($content) |
| 204 |
{ |
| 205 |
$taboola_content_mid = array(); |
| 206 |
if ($this->should_show_content_widget_mid()){ |
| 207 |
|
| 208 |
$secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->mid_widget_id, |
| 209 |
'{{CONTAINER}}' => 'taboola-mid-article-thumbnails', |
| 210 |
'{{PLACEMENT}}' => $this->settings->mid_placement); |
| 211 |
|
| 212 |
$secondWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$secondWidgetParams); |
| 213 |
$taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-mid-article-thumbnails'></div>"; |
| 214 |
$taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $secondWidgetScript; |
| 215 |
|
| 216 |
$content = $this->embed_taboola_content_location_mid($content,$taboola_content_mid,trim($this->settings->mid_location_string)); |
| 217 |
} |
| 218 |
|
| 219 |
return $content; |
| 220 |
} |
| 221 |
|
| 222 |
// Homepage widget |
| 223 |
function load_taboola_content_home($content) |
| 224 |
{ |
| 225 |
|
| 226 |
$taboola_content_home = array(); |
| 227 |
if ($this->should_show_content_widget_home()){ |
| 228 |
|
| 229 |
$homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id, |
| 230 |
'{{CONTAINER}}' => 'taboola-homepage-thumbnails', |
| 231 |
'{{PLACEMENT}}' => $this->settings->home_placement); |
| 232 |
|
| 233 |
$homeWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$homeWidgetParams); |
| 234 |
$taboola_content_home[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-homepage-thumbnails'></div>"; |
| 235 |
$taboola_content_home[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $homeWidgetScript; |
| 236 |
|
| 237 |
$content = $this->embed_taboola_content_location_home($content,$taboola_content_home,trim($this->settings->home_location_string)); |
| 238 |
} |
| 239 |
return $content; |
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
// Below-article widget |
| 244 |
|
| 245 |
// Extract the taboola content in the required format: |
| 246 |
// String - for injecting on the servr side |
| 247 |
// Script or HTML - for injecting on the client side |
| 248 |
function format_taboola_content($taboola_content,$format){ |
| 249 |
$ret_val = null; |
| 250 |
|
| 251 |
switch($format){ |
| 252 |
case TABOOLA_CONTENT_FORMAT_STRING: |
| 253 |
$result_string = join("",$taboola_content[TABOOLA_CONTENT_FORMAT_HTML]). |
| 254 |
"<script type='text/javascript'>".join("\n",$taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>"; |
| 255 |
$ret_val = $result_string; |
| 256 |
break; |
| 257 |
|
| 258 |
// script or html |
| 259 |
default: |
| 260 |
$ret_val = str_replace("\n","",join("",$taboola_content[$format])); |
| 261 |
break; |
| 262 |
} |
| 263 |
|
| 264 |
return $ret_val; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
// Mid-article widget |
| 269 |
function format_taboola_content_mid($taboola_content_mid,$format){ |
| 270 |
$ret_val = null; |
| 271 |
|
| 272 |
switch($format){ |
| 273 |
case TABOOLA_CONTENT_FORMAT_STRING: |
| 274 |
$result_string = join("",$taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML]). |
| 275 |
"<script type='text/javascript'>".join("\n",$taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>"; |
| 276 |
$ret_val = $result_string; |
| 277 |
break; |
| 278 |
|
| 279 |
// script or html |
| 280 |
default: |
| 281 |
$ret_val = str_replace("\n","",join("",$taboola_content_mid[$format])); |
| 282 |
break; |
| 283 |
} |
| 284 |
|
| 285 |
return $ret_val; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
// Homepage widget |
| 290 |
function format_taboola_content_home($taboola_content_home,$format){ |
| 291 |
|
| 292 |
$ret_val = null; |
| 293 |
|
| 294 |
switch($format){ |
| 295 |
case TABOOLA_CONTENT_FORMAT_STRING: |
| 296 |
$result_string = join("",$taboola_content_home[TABOOLA_CONTENT_FORMAT_HTML]). |
| 297 |
"<script type='text/javascript'>".join("\n",$taboola_content_home[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>"; |
| 298 |
$ret_val = $result_string; |
| 299 |
break; |
| 300 |
|
| 301 |
// script or html |
| 302 |
default: |
| 303 |
$ret_val = str_replace("\n","",join("",$taboola_content_home[$format])); |
| 304 |
break; |
| 305 |
} |
| 306 |
|
| 307 |
return $ret_val; |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
// Below-article widget |
| 312 |
// Do the actual logic of choosing where to place the taboola content. |
| 313 |
function embed_taboola_content_location($content, $taboola_content){ |
| 314 |
$do_default = true; |
| 315 |
|
| 316 |
// tag is placed outside of content in order to allow "read more" functionality. |
| 317 |
if ($this->settings->out_of_content_enabled){ |
| 318 |
|
| 319 |
$scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( |
| 320 |
"{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML), |
| 321 |
"{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT)) |
| 322 |
); |
| 323 |
$scriptWrapper->appendScript("injectWidgetByMarker('tbmarker');"); |
| 324 |
$content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$scriptWrapper."</script>"; |
| 325 |
$do_default = false; |
| 326 |
} |
| 327 |
|
| 328 |
// Default for below-article widget - add to the end of the content |
| 329 |
if ($do_default){ |
| 330 |
$content = $content.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING); |
| 331 |
} |
| 332 |
|
| 333 |
return $content; |
| 334 |
} |
| 335 |
|
| 336 |
// Mid-article widget |
| 337 |
// Do the actual logic of choosing where to place the taboola content based on the "location" attribute |
| 338 |
function embed_taboola_content_location_mid($content, $taboola_content_mid, $location){ |
| 339 |
$do_default = true; |
| 340 |
|
| 341 |
if (isset($location) && $location != ''){ |
| 342 |
$first_char = substr($location,0,1); |
| 343 |
|
| 344 |
// DIV/XPATH provided for JS handling |
| 345 |
if ($first_char == TABOOLA_JS_MARKER){ |
| 346 |
$full_indicator = substr($location,0,strlen(TABOOLA_JS_INDICATOR)); |
| 347 |
|
| 348 |
if ($full_indicator == TABOOLA_JS_INDICATOR){ |
| 349 |
|
| 350 |
$xpath = substr($location,strlen(TABOOLA_JS_INDICATOR)); |
| 351 |
$scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( |
| 352 |
"{{HTML}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_HTML), |
| 353 |
"{{SCRIPT}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_SCRIPT)) |
| 354 |
); |
| 355 |
$scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');"); |
| 356 |
$content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>"; |
| 357 |
|
| 358 |
$do_default = false; |
| 359 |
} |
| 360 |
|
| 361 |
// server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm) |
| 362 |
// basically it's CSS selectors like in jQuery |
| 363 |
} else{ |
| 364 |
require_once('simple_html_dom.php'); |
| 365 |
|
| 366 |
$html_doc = str_get_html($content); |
| 367 |
$target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1); |
| 368 |
|
| 369 |
// if the location was found within the html content |
| 370 |
if (isset($target_location) && is_object($target_location)){ |
| 371 |
|
| 372 |
// adding taboola content AFTER the target location |
| 373 |
$target_location->outertext = $target_location->outertext.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING); |
| 374 |
$content = $html_doc; |
| 375 |
$do_default = false; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
// Default for below-article widget - add to the end of the content |
| 380 |
// if ($do_default){ |
| 381 |
// $content = $content.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING); |
| 382 |
// } |
| 383 |
|
| 384 |
return $content; |
| 385 |
} |
| 386 |
|
| 387 |
// Homepage widget |
| 388 |
// Do the actual logic of choosing where to place the taboola content based on the "location" attribute |
| 389 |
function embed_taboola_content_location_home($content, $taboola_content_home, $location){ |
| 390 |
|
| 391 |
$do_default = true; |
| 392 |
|
| 393 |
if (isset($location) && $location != ''){ |
| 394 |
$first_char = substr($location,0,1); |
| 395 |
|
| 396 |
// DIV/XPATH provided for JS handling |
| 397 |
if ($first_char == TABOOLA_JS_MARKER){ |
| 398 |
$full_indicator = substr($location,0,strlen(TABOOLA_JS_INDICATOR)); |
| 399 |
|
| 400 |
if ($full_indicator == TABOOLA_JS_INDICATOR){ |
| 401 |
|
| 402 |
$xpath = substr($location,strlen(TABOOLA_JS_INDICATOR)); |
| 403 |
$scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( |
| 404 |
"{{HTML}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_HTML), |
| 405 |
"{{SCRIPT}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_SCRIPT)) |
| 406 |
); |
| 407 |
$scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');"); |
| 408 |
$content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>"; |
| 409 |
|
| 410 |
$do_default = false; |
| 411 |
} |
| 412 |
|
| 413 |
// server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm) |
| 414 |
// basically it's CSS selectors like in jQuery |
| 415 |
} else{ |
| 416 |
require_once('simple_html_dom.php'); |
| 417 |
|
| 418 |
$html_doc = str_get_html($content); |
| 419 |
$target_location = $html_doc->find($location,($this->settings->home_location_string_occurrence)-1); |
| 420 |
|
| 421 |
// if the location was found within the html content |
| 422 |
if (isset($target_location) && is_object($target_location)){ |
| 423 |
|
| 424 |
// adding taboola content AFTER the target location |
| 425 |
$target_location->outertext = $target_location->outertext.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING); |
| 426 |
$content = $html_doc; |
| 427 |
$do_default = false; |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
// 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 |
// } |
| 435 |
return $content; |
| 436 |
|
| 437 |
} |
| 438 |
|
| 439 |
function admin_generate_menu(){ |
| 440 |
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); |
| 442 |
} |
| 443 |
|
| 444 |
function admin_taboola_settings(){ |
| 445 |
|
| 446 |
global $wpdb; |
| 447 |
$settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 448 |
$taboola_errors = array(); |
| 449 |
if($_SERVER['REQUEST_METHOD'] == 'POST'){ |
| 450 |
|
| 451 |
if(trim(strip_tags($_POST['publisher_id'])) == ''){ |
| 452 |
$taboola_errors[] = "Publisher ID"; |
| 453 |
} |
| 454 |
|
| 455 |
if(isset($_POST['first_bc_enabled'])) { |
| 456 |
if (trim(strip_tags($_POST['first_bc_widget_id'])) == '') { |
| 457 |
$taboola_errors[] = "Below-article > Widget ID"; |
| 458 |
} |
| 459 |
if (trim(strip_tags($_POST['first_bc_placement'])) == '') { |
| 460 |
$taboola_errors[] = "Below-article > Placement Name"; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
if(isset($_POST['mid_enabled'])) { |
| 465 |
if (trim(strip_tags($_POST['mid_widget_id'])) == '') { |
| 466 |
$taboola_errors[] = "Mid-article > Widget ID"; |
| 467 |
} |
| 468 |
if (trim(strip_tags($_POST['mid_placement'])) == '') { |
| 469 |
$taboola_errors[] = "Mid-article > Placement Name"; |
| 470 |
} |
| 471 |
if (trim(strip_tags($_POST['mid_location_string'])) == '') { |
| 472 |
$taboola_errors[] = "Mid-article > CSS selector"; |
| 473 |
} |
| 474 |
else { |
| 475 |
// If the 'location' WAS filled in, then... |
| 476 |
|
| 477 |
// 1) Check for a valid 'location': |
| 478 |
|
| 479 |
// Validation method has not been implemented |
| 480 |
// mid_if (!$this->is_locaton_string_valid1($_POST['mid_location_string'])) { |
| 481 |
// $taboola_errors[] = "Mid-article > CSS Selector (invalid value)"; |
| 482 |
// } |
| 483 |
|
| 484 |
// 2) Validate the 'occurrence': |
| 485 |
if (!$this->is_mid_location_string_occurrence_valid($_POST['mid_location_string_occurrence'])) { |
| 486 |
$taboola_errors[] = "Mid-article > Occurance (must be >= 1)"; |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
} |
| 491 |
|
| 492 |
if(isset($_POST['home_enabled'])) { |
| 493 |
if (trim(strip_tags($_POST['home_widget_id'])) == '') { |
| 494 |
$taboola_errors[] = "Homepage > Widget ID"; |
| 495 |
} |
| 496 |
if (trim(strip_tags($_POST['home_placement'])) == '') { |
| 497 |
$taboola_errors[] = "Homepage > Placement Name"; |
| 498 |
} |
| 499 |
|
| 500 |
if (trim(strip_tags($_POST['home_location_string'])) == '') { |
| 501 |
$taboola_errors[] = "Homepage > CSS selector"; |
| 502 |
} |
| 503 |
else { |
| 504 |
// If the 'location' WAS filled in, then... |
| 505 |
|
| 506 |
// 1) Check for a valid 'location': |
| 507 |
|
| 508 |
// Validation method has not been implemented |
| 509 |
// if (!$this->is_home_location_string_valid($_POST['home_location_string'])) { |
| 510 |
// $taboola_errors[] = "Homepage > CSS Selector (invalid value)"; |
| 511 |
// } |
| 512 |
|
| 513 |
// 2) Validate the 'occurrence': |
| 514 |
if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) { |
| 515 |
$taboola_errors[] = "Homepage > Occurance (must be >= 1)"; |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
if(count($taboola_errors) == 0){ |
| 521 |
|
| 522 |
$data = array( |
| 523 |
"publisher_id" => trim($_POST['publisher_id']), |
| 524 |
|
| 525 |
"first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false, |
| 526 |
"first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '', |
| 527 |
"first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '', |
| 528 |
|
| 529 |
"out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false, |
| 530 |
|
| 531 |
"mid_enabled" => isset($_POST['mid_enabled']) ? true : false, |
| 532 |
"mid_widget_id" => !empty($_POST['mid_widget_id']) ? trim($_POST['mid_widget_id']) : '', |
| 533 |
"mid_placement" => !empty($_POST['mid_placement']) ? trim($_POST['mid_placement']) : '', |
| 534 |
"mid_paragraph_ui_mode" => !empty($_POST['mid_paragraph_ui_mode']) ? trim($_POST['mid_paragraph_ui_mode']) : '', |
| 535 |
|
| 536 |
"mid_location_string_occurrence" => !empty($_POST['mid_location_string_occurrence']) ? $_POST['mid_location_string_occurrence'] : '', |
| 537 |
"mid_location_string" => !empty($_POST['mid_location_string']) ? trim($_POST['mid_location_string']) : '', |
| 538 |
|
| 539 |
"home_enabled" => isset($_POST['home_enabled']) ? true : false, |
| 540 |
"home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '', |
| 541 |
"home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '', |
| 542 |
|
| 543 |
"home_location_string_occurrence" => !empty($_POST['home_location_string_occurrence']) ? $_POST['home_location_string_occurrence'] : '', |
| 544 |
"home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : '' |
| 545 |
); |
| 546 |
|
| 547 |
//var_dump($settings); |
| 548 |
|
| 549 |
$is_valid_nonce = false; |
| 550 |
|
| 551 |
if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) { |
| 552 |
$is_valid_nonce = true; |
| 553 |
} |
| 554 |
|
| 555 |
if ($is_valid_nonce) { |
| 556 |
if($settings == NULL){ |
| 557 |
$wpdb->insert($this->tbl_taboola_settings, $data, null, null); |
| 558 |
} else { |
| 559 |
$wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id)); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
} |
| 564 |
$settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 565 |
} |
| 566 |
|
| 567 |
include_once('settings.php'); |
| 568 |
} |
| 569 |
|
| 570 |
function is_mid_location_string_valid($mid_location_string){ |
| 571 |
// TODO:: validate the location string |
| 572 |
return true; |
| 573 |
} |
| 574 |
|
| 575 |
function is_mid_location_string_occurrence_valid($mid_location_string_occurrence){ |
| 576 |
|
| 577 |
if ((int)$mid_location_string_occurrence >= 1) { |
| 578 |
return true; |
| 579 |
} |
| 580 |
return false; |
| 581 |
} |
| 582 |
|
| 583 |
function is_home_location_string_valid($home_location_string){ |
| 584 |
// TODO:: validate the location string |
| 585 |
return true; |
| 586 |
} |
| 587 |
|
| 588 |
function is_home_location_string_occurrence_valid($home_location_string_occurrence){ |
| 589 |
|
| 590 |
if ((int)$home_location_string_occurrence >= 1) { |
| 591 |
return true; |
| 592 |
} |
| 593 |
return false; |
| 594 |
} |
| 595 |
function columnExists($column_name) { |
| 596 |
global $wpdb; |
| 597 |
$table_name = $wpdb->prefix . "_taboola_settings"; |
| 598 |
tb_write_log("table name : " . $table_name); |
| 599 |
tb_write_log("column name : " . $column_name); |
| 600 |
|
| 601 |
$column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE '$column_name'") !== null; |
| 602 |
|
| 603 |
if ($column_exists) { |
| 604 |
tb_write_log("Yes - column " . $column_name . " DOES exist!"); //PC |
| 605 |
return true; |
| 606 |
} else { |
| 607 |
tb_write_log("No - column " . $column_name . " does NOT exist!"); //PC |
| 608 |
return false; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
function is_upgrade_from_v1() { |
| 613 |
|
| 614 |
if($this->columnExists("first_bc_widget_id") && !$this->columnExists("first_bc_placement")){ |
| 615 |
tb_write_log("This IS a v1 upgrade!"); //PC |
| 616 |
return true; |
| 617 |
} |
| 618 |
else { |
| 619 |
tb_write_log("This is NOT a v1 upgrade!"); //PC |
| 620 |
return false; |
| 621 |
} |
| 622 |
|
| 623 |
} |
| 624 |
|
| 625 |
function is_db_updated_for_min_ver($min_ver) { |
| 626 |
/** |
| 627 |
* Checks if the DB was *previously* updated for the minimum version specified. |
| 628 |
* $min_ver should be passed in a format like this: "2.1.0" |
| 629 |
*/ |
| 630 |
|
| 631 |
global $wpdb; |
| 632 |
|
| 633 |
// PC - Temporary patch for v2.1.0 |
| 634 |
|
| 635 |
// START patch =============================================== |
| 636 |
// Check if the `_taboola` table exists. |
| 637 |
// If not, then return false. |
| 638 |
|
| 639 |
$table_name = $wpdb->prefix . "_taboola_settings"; |
| 640 |
|
| 641 |
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { |
| 642 |
tb_write_log("Table NOT FOUND"); |
| 643 |
return false; |
| 644 |
} |
| 645 |
else { |
| 646 |
tb_write_log("Table EXISTS"); |
| 647 |
} |
| 648 |
// END patch ================================================= |
| 649 |
|
| 650 |
// Check the saved version in wp_options. Default to '1.0.0' if the option is not found. |
| 651 |
$saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0'); |
| 652 |
|
| 653 |
$db_is_up_to_date = version_compare($saved_version, $min_ver, '>='); |
| 654 |
//tb_write_log($db_is_up_to_date); |
| 655 |
return $db_is_up_to_date; |
| 656 |
|
| 657 |
} |
| 658 |
|
| 659 |
function is_db_updated_for_current_ver() { |
| 660 |
/** |
| 661 |
* DEPRECATED |
| 662 |
* Checks if the DB was *previously* updated for the version currently loaded. |
| 663 |
*/ |
| 664 |
global $wpdb; |
| 665 |
|
| 666 |
// Check the saved version in wp_options. Default to '1.0.0' if the option is not found. |
| 667 |
$saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0'); |
| 668 |
|
| 669 |
// Get the currently loaded plugin version. |
| 670 |
$plugin_version = $this->get_loaded_plugin_version(); |
| 671 |
|
| 672 |
$db_is_up_to_date = version_compare($saved_version, $plugin_version, '>='); |
| 673 |
//tb_write_log($db_is_up_to_date); |
| 674 |
return $db_is_up_to_date; |
| 675 |
|
| 676 |
} |
| 677 |
|
| 678 |
function get_loaded_plugin_version() { |
| 679 |
|
| 680 |
$plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only |
| 681 |
$loaded_plugin_version = $plugin_data['Version']; |
| 682 |
return $loaded_plugin_version; |
| 683 |
|
| 684 |
} |
| 685 |
|
| 686 |
function save_taboola_version($min_ver) { |
| 687 |
/** |
| 688 |
* Checks for the saved version in wp_options. |
| 689 |
* If not found - adds it. Else - updates it. |
| 690 |
*/ |
| 691 |
|
| 692 |
|
| 693 |
tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC |
| 694 |
|
| 695 |
$saved_version = get_option(TABOOLA_OPTION_NAME, ''); |
| 696 |
if ($saved_version == '') { |
| 697 |
add_option(TABOOLA_OPTION_NAME, $min_ver); |
| 698 |
} |
| 699 |
else { |
| 700 |
update_option(TABOOLA_OPTION_NAME, $min_ver); |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
function update_db(){ |
| 705 |
|
| 706 |
// If we are up to date, then skip this method... |
| 707 |
if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) { |
| 708 |
//tb_write_log("All up to date!"); |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
$this->save_taboola_version(TABOOLA_MIN_VER); |
| 713 |
|
| 714 |
global $wpdb; |
| 715 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 716 |
|
| 717 |
|
| 718 |
// Handling for older MySQL versions |
| 719 |
if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) { |
| 720 |
if (!empty($wpdb->charset)) |
| 721 |
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; |
| 722 |
if (!empty($wpdb->collate)) |
| 723 |
$charset_collate .= " COLLATE $wpdb->collate"; |
| 724 |
} |
| 725 |
|
| 726 |
// Check if this is an upgrade from v1 |
| 727 |
$is_upgrade_from_v1 = $this->is_upgrade_from_v1(); |
| 728 |
|
| 729 |
// tb_write_log("Is upgrade from v1: " . ($is_upgrade_from_v1 ? 'true' : 'false')); |
| 730 |
|
| 731 |
|
| 732 |
//settings table structure |
| 733 |
$sql_table_settings = " |
| 734 |
CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` ( |
| 735 |
`id` INT NOT NULL AUTO_INCREMENT , |
| 736 |
`publisher_id` VARCHAR(255) DEFAULT NULL, |
| 737 |
`first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 738 |
`first_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 739 |
`first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .", |
| 740 |
`first_bc_custom_css` TEXT DEFAULT NULL, |
| 741 |
`second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 742 |
`second_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 743 |
`second_bc_custom_css` TEXT DEFAULT NULL, |
| 744 |
`location_string` TEXT DEFAULT NULL, |
| 745 |
`mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 746 |
`mid_widget_id` VARCHAR(255) DEFAULT NULL, |
| 747 |
`mid_placement` VARCHAR(255) DEFAULT NULL, |
| 748 |
`out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE, |
| 749 |
`mid_location_string` TEXT DEFAULT NULL, |
| 750 |
`mid_location_string_occurrence` SMALLINT DEFAULT NULL, |
| 751 |
`mid_paragraph_ui_mode` VARCHAR(255) DEFAULT NULL, |
| 752 |
`home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 753 |
`home_widget_id` VARCHAR(255) DEFAULT NULL, |
| 754 |
`home_placement` VARCHAR(255) DEFAULT NULL, |
| 755 |
`home_location_string` TEXT DEFAULT NULL, |
| 756 |
`home_location_string_occurrence` SMALLINT DEFAULT NULL, |
| 757 |
PRIMARY KEY (`id`) |
| 758 |
)" . $charset_collate . ";"; |
| 759 |
|
| 760 |
// tb_write_log($sql_table_settings); |
| 761 |
|
| 762 |
// create/update the table |
| 763 |
dbDelta($sql_table_settings); |
| 764 |
} |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
global $taboolaWP; |
| 769 |
$taboolaWP = new TaboolaWP(); |
| 770 |
|
| 771 |
/* |
| 772 |
A few utility methods for logging |
| 773 |
*/ |
| 774 |
|
| 775 |
// Writes debugging messages to 'logs/debug.log'. |
| 776 |
// The exact file location depends on *where* you trigger the function. |
| 777 |
// In this script, we trigger the function from the admin dashboard. |
| 778 |
// So the log file is located under 'wp-admin/logs'. |
| 779 |
function tb_write_log($log_msg) |
| 780 |
{ |
| 781 |
$log_filename = "logs"; |
| 782 |
if (!file_exists($log_filename)) |
| 783 |
{ |
| 784 |
mkdir($log_filename, 0777, true); |
| 785 |
} |
| 786 |
$log_file_data = $log_filename.'/debug.log'; |
| 787 |
|
| 788 |
// Add date/time |
| 789 |
$date = date('Y-m-d H:i:s'); |
| 790 |
$log_msg_with_date = $date." : ".$log_msg; |
| 791 |
|
| 792 |
if (TABOOLA_DEBUG_MODE) |
| 793 |
file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND); |
| 794 |
|
| 795 |
} |
| 796 |
|
| 797 |
// Write to console |
| 798 |
function tb_console_log( $data ){ |
| 799 |
|
| 800 |
If(wp_get_environment_type() === 'development') { |
| 801 |
|
| 802 |
echo '<script>'; |
| 803 |
echo 'console.log('. json_encode( $data ) .')'; |
| 804 |
echo '</script>'; |
| 805 |
|
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
// Write to webpage and stop |
| 810 |
function tb_print_to_page($data) { |
| 811 |
If(wp_get_environment_type() === 'development') { |
| 812 |
print_r($data); |
| 813 |
die; |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
// |
| 818 |
|