PluginProbe
Taboola / 1.0.5
Taboola v1.0.5
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
taboola / taboola_widget.php

taboola_widget.php in Taboola 1.0.5, at taboola_widget.php

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