PluginProbe
Taboola / 1.0.8
Taboola v1.0.8
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.8, at taboola_widget.php

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