| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Taboola |
| 4 |
* Plugin URI: http://taboola.com |
| 5 |
* Description: Taboola |
| 6 |
* Version: 1.0.1 |
| 7 |
* Author: Taboola |
| 8 |
*/ |
| 9 |
|
| 10 |
include_once('widget.php'); |
| 11 |
|
| 12 |
if (!class_exists('TaboolaWP')) { |
| 13 |
class TaboolaWP |
| 14 |
{ |
| 15 |
//save internal data |
| 16 |
public $data = array(); |
| 17 |
public $_is_widget_on_page; |
| 18 |
public $_is_head_script_loaded = false; |
| 19 |
|
| 20 |
function TaboolaWP() |
| 21 |
{ |
| 22 |
global $wpdb; |
| 23 |
|
| 24 |
//initialize plugin constant |
| 25 |
DEFINE('TaboolaWP', true); |
| 26 |
|
| 27 |
$this->_is_widget_on_page = false; |
| 28 |
$this->_is_head_script_loaded = false; |
| 29 |
|
| 30 |
$this->plugin_name = plugin_basename(__FILE__); |
| 31 |
$this->plugin_directory = plugin_dir_path(__FILE__); |
| 32 |
$this->plugin_url = trailingslashit(WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__))); |
| 33 |
$this->settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 34 |
|
| 35 |
$this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings'; |
| 36 |
|
| 37 |
//activation function |
| 38 |
register_activation_hook($this->plugin_name, array(&$this, 'activate')); |
| 39 |
|
| 40 |
// Enable sidebar widgets |
| 41 |
if($this->settings != NULL && !empty($this->settings->publisher_id)){ |
| 42 |
//register Taboola widget |
| 43 |
add_action('widgets_init', |
| 44 |
create_function('', 'return register_widget("WP_Widget_Taboola");') |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
if (is_admin()) { |
| 49 |
//add menu for plugin |
| 50 |
add_action( 'admin_menu', array(&$this, 'admin_generate_menu') ); |
| 51 |
add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 ); |
| 52 |
} else { |
| 53 |
if($this->settings != NULL){ |
| 54 |
add_action('wp_head', array(&$this, 'taboola_header_loader_inject')); |
| 55 |
add_action('wp_footer', array(&$this, 'taboola_footer_loader_js')); |
| 56 |
add_filter('the_content', array(&$this, 'load_taboola_content')); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
function plugin_action_links($links, $file) { |
| 65 |
static $this_plugin; |
| 66 |
|
| 67 |
if (!$this_plugin) { |
| 68 |
$this_plugin = plugin_basename(__FILE__); |
| 69 |
} |
| 70 |
|
| 71 |
if ($file == $this_plugin) { |
| 72 |
$settings_link = '<a href="' . get_bloginfo('wpurl') . '/wp-admin/options-general.php?page=taboola_widget">Settings</a>'; |
| 73 |
array_unshift($links, $settings_link); |
| 74 |
} |
| 75 |
|
| 76 |
return $links; |
| 77 |
} |
| 78 |
|
| 79 |
private function should_show_content_widget(){ |
| 80 |
$retVal = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->first_bc_enabled && trim($this->settings->first_bc_widget_id) != ''); |
| 81 |
return $retVal; |
| 82 |
} |
| 83 |
|
| 84 |
private function should_show_sidebar_widget(){ |
| 85 |
$retVal = ((trim($this->settings->publisher_id) != '') && is_active_widget( false, false, TABOOLA_WIDGET_BASE_ID, true )); |
| 86 |
return $retVal; |
| 87 |
} |
| 88 |
|
| 89 |
// Determine if a taboola widget should be added somewhere on the current page (content or sidebar) |
| 90 |
function is_widget_on_page(){ |
| 91 |
return $this->should_show_content_widget() || $this->should_show_sidebar_widget(); |
| 92 |
} |
| 93 |
|
| 94 |
function get_page_type(){ |
| 95 |
|
| 96 |
$page_type='article'; |
| 97 |
if (is_front_page()){ |
| 98 |
$page_type='home'; |
| 99 |
}else if (is_category() || is_archive() || is_search()){ |
| 100 |
$page_type='category'; |
| 101 |
} |
| 102 |
return $page_type; |
| 103 |
} |
| 104 |
|
| 105 |
// return the head loader script |
| 106 |
function taboola_header_loader_js() { |
| 107 |
$head_string = ""; |
| 108 |
|
| 109 |
// Only adding Script1 if a widget is going to be placed on the page. |
| 110 |
if ($this->is_widget_on_page()){ |
| 111 |
|
| 112 |
$script1_content = str_replace('{{PUBLISHER_ID}}', $this->settings->publisher_id, file_get_contents($this->plugin_directory.'js/script1.js')); |
| 113 |
$script1_content = str_replace('{{PAGE_TYPE}}',$this->get_page_type(),$script1_content); |
| 114 |
|
| 115 |
$head_string = '<script type="text/javascript">'.$script1_content.'</script>'; |
| 116 |
} |
| 117 |
|
| 118 |
return $head_string; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
// This function is used for the hook action, injects the header content to the <head> tag. |
| 123 |
function taboola_header_loader_inject(){ |
| 124 |
|
| 125 |
echo $this->taboola_header_loader_js(); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
function taboola_footer_loader_js() { |
| 130 |
|
| 131 |
// Only adding Script1 if a widget is going to be placed on the page. |
| 132 |
if ( $this->is_widget_on_page() ){ |
| 133 |
$script3_content = file_get_contents($this->plugin_directory.'js/script3.js'); |
| 134 |
echo '<script type="text/javascript">'.$script3_content.'</script>'; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
function load_taboola_content($content) |
| 139 |
{ |
| 140 |
|
| 141 |
if ($this->should_show_content_widget()){ |
| 142 |
|
| 143 |
$script2_content = str_replace('{{WIDGET_ID}}', $this->settings->first_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js')); |
| 144 |
$script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article', $script2_content); |
| 145 |
$script2_content = str_replace('{{PLACEMENT}}', 'below-article', $script2_content); |
| 146 |
|
| 147 |
$content = $content . |
| 148 |
'<div id="taboola-below-article"></div>' . |
| 149 |
'<script type="text/javascript">'.$script2_content.'</script>'; |
| 150 |
|
| 151 |
// Adding the 2nd widget if needed |
| 152 |
if($this->settings->second_bc_enabled && trim($this->settings->second_bc_widget_id) != ''){ |
| 153 |
$script2_content = str_replace('{{WIDGET_ID}}', $this->settings->second_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js')); |
| 154 |
$script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article-second', $script2_content); |
| 155 |
$script2_content = str_replace('{{PLACEMENT}}', 'below-article-2nd', $script2_content); |
| 156 |
|
| 157 |
$content = $content . |
| 158 |
'<div id="taboola-below-article-second"></div>' . |
| 159 |
'<script type="text/javascript">'.$script2_content.'</script>'; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return $content; |
| 164 |
} |
| 165 |
|
| 166 |
function admin_generate_menu(){ |
| 167 |
global $current_user; |
| 168 |
add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110); |
| 169 |
} |
| 170 |
|
| 171 |
function admin_taboola_settings(){ |
| 172 |
global $wpdb; |
| 173 |
$settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 174 |
$taboola_errors = array(); |
| 175 |
if($_SERVER['REQUEST_METHOD'] == 'POST'){ |
| 176 |
|
| 177 |
if(trim($_POST['publisher_id']) == ''){ |
| 178 |
$taboola_errors[] = "Please add a 'Publisher ID' in order to apply changes to your widgets"; |
| 179 |
} |
| 180 |
if(($_POST['first_bc_enabled'] == 'on' && trim($_POST['first_bc_widget_id']) == '') || |
| 181 |
($_POST['second_bc_enabled'] == 'on' && trim($_POST['second_bc_widget_id']) == '') |
| 182 |
){ |
| 183 |
$taboola_errors[] = "Please add a 'Widget ID' in order to apply changes to your widgets"; |
| 184 |
} |
| 185 |
if(count($taboola_errors) == 0){ |
| 186 |
|
| 187 |
$data = array( |
| 188 |
"publisher_id" => trim($_POST['publisher_id']), |
| 189 |
"first_bc_enabled" => $_POST['first_bc_enabled'] == 'on' ? true : false, |
| 190 |
"first_bc_widget_id" => trim($_POST['first_bc_widget_id']), |
| 191 |
"second_bc_enabled" => $_POST['second_bc_enabled'] == 'on' ? true : false, |
| 192 |
"second_bc_widget_id" => trim($_POST['second_bc_widget_id']), |
| 193 |
); |
| 194 |
//var_dump($settings); |
| 195 |
if($settings == NULL){ |
| 196 |
$wpdb->insert($this->tbl_taboola_settings, $data, null, null); |
| 197 |
} else { |
| 198 |
$wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id)); |
| 199 |
} |
| 200 |
} |
| 201 |
$settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
include_once('settings.php'); |
| 206 |
} |
| 207 |
function activate(){ |
| 208 |
global $wpdb; |
| 209 |
|
| 210 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 211 |
|
| 212 |
$settings_table = $this->tbl_taboola_settings; |
| 213 |
|
| 214 |
//check mysql version |
| 215 |
if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) { |
| 216 |
if (!empty($wpdb->charset)) |
| 217 |
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; |
| 218 |
if (!empty($wpdb->collate)) |
| 219 |
$charset_collate .= " COLLATE $wpdb->collate"; |
| 220 |
} |
| 221 |
|
| 222 |
//settings table structure |
| 223 |
$sql_table_settings = " |
| 224 |
CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` ( |
| 225 |
`id` INT NOT NULL AUTO_INCREMENT , |
| 226 |
`publisher_id` VARCHAR(255) DEFAULT NULL, |
| 227 |
`first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 228 |
`first_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 229 |
`first_bc_custom_css` TEXT DEFAULT NULL, |
| 230 |
`second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 231 |
`second_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 232 |
`second_bc_custom_css` TEXT DEFAULT NULL, |
| 233 |
PRIMARY KEY (`id`) |
| 234 |
)" . $charset_collate . ";"; |
| 235 |
|
| 236 |
//check if table exists |
| 237 |
if ($wpdb->get_var("show tables like '" . $settings_table . "'") != $settings_table) { |
| 238 |
dbDelta($sql_table_settings); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
global $taboolaWP; |
| 245 |
$taboolaWP = new TaboolaWP(); |