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