PluginProbe
Taboola / 1.0.10
Taboola v1.0.10
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 +212 -35 1.01.0.10 View file →
@@ -2,27 +2,43 @@
2 2 /**
3 3 * Plugin Name: Taboola
4 4 * Plugin URI: http://taboola.com
5 5 * Description: Taboola
6 - * Version: 1.0
6 + * Version: 1.0.10
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.10");
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
30 +
19 31 function TaboolaWP()
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,18 @@
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 55 create_function('', 'return register_widget("WP_Widget_Taboola");')
41 56 );
42 - }*/
57 + }
43 58
59 + $this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled;
60 +
44 61 if (is_admin()) {
45 62 //add menu for plugin
46 63 add_action( 'admin_menu', array(&$this, 'admin_generate_menu') );
47 64 add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
@@ -46,14 +63,18 @@
46 63 add_action( 'admin_menu', array(&$this, 'admin_generate_menu') );
47 64 add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
48 65 } else {
49 66 if($this->settings != NULL){
50 - add_action('wp_head', array(&$this, 'taboola_header_loader_js'));
67 + add_action('wp_head', array(&$this, 'taboola_header_loader_inject'));
51 68 add_action('wp_footer', array(&$this, 'taboola_footer_loader_js'));
52 69 add_filter('the_content', array(&$this, 'load_taboola_content'));
53 70 }
54 71 }
55 72 }
73 +
74 +
75 +
76 +
56 77 function plugin_action_links($links, $file) {
57 78 static $this_plugin;
58 79
59 80 if (!$this_plugin) {
@@ -66,47 +87,192 @@
66 87 }
67 88
68 89 return $links;
69 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
70 119 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>';
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;
73 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 +
74 147 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>';
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 + }
77 154 }
78 155
79 156 function load_taboola_content($content)
80 157 {
81 - if (trim($this->settings->publisher_id) == ''){
82 - return $content;
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));
83 182 }
84 - if (is_single()) {
85 183
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);
184 + return $content;
185 + }
90 186
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 187
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>';
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;
102 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 + }
103 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;
104 264 }
105 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 +
106 271 return $content;
107 272 }
108 273
274 +
109 275 function admin_generate_menu(){
110 276 global $current_user;
111 277 add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110);
112 278 }
@@ -124,8 +290,11 @@
124 290 ($_POST['second_bc_enabled'] == 'on' && trim($_POST['second_bc_widget_id']) == '')
125 291 ){
126 292 $taboola_errors[] = "Please add a 'Widget ID' in order to apply changes to your widgets";
127 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 + }
128 297 if(count($taboola_errors) == 0){
129 298
130 299 $data = array(
131 300 "publisher_id" => trim($_POST['publisher_id']),
@@ -130,13 +299,14 @@
130 299 $data = array(
131 300 "publisher_id" => trim($_POST['publisher_id']),
132 301 "first_bc_enabled" => $_POST['first_bc_enabled'] == 'on' ? true : false,
133 302 "first_bc_widget_id" => trim($_POST['first_bc_widget_id']),
134 - "first_bc_custom_css" => trim($_POST['first_bc_custom_css']),
135 303 "second_bc_enabled" => $_POST['second_bc_enabled'] == 'on' ? true : false,
136 304 "second_bc_widget_id" => trim($_POST['second_bc_widget_id']),
137 - "second_bc_custom_css" => trim($_POST['second_bc_custom_css'])
305 + "location_string" => trim($_POST['location_string']),
306 + "out_of_content_enabled" => $_POST['out_of_content_enabled'] == 'on' ? true : false
138 307 );
308 +
139 309 //var_dump($settings);
140 310 if($settings == NULL){
141 311 $wpdb->insert($this->tbl_taboola_settings, $data, null, null);
142 312 } else {
@@ -148,8 +318,13 @@
148 318
149 319
150 320 include_once('settings.php');
151 321 }
322 +
323 + function is_location_string_valid($location_string){
324 + // TODO:: validate the location string
325 + return true;
326 + }
152 327 function activate(){
153 328 global $wpdb;
154 329
155 330 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
@@ -156,9 +331,9 @@
156 331
157 332 $settings_table = $this->tbl_taboola_settings;
158 333
159 334 //check mysql version
160 - if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
335 + if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
161 336 if (!empty($wpdb->charset))
162 337 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
163 338 if (!empty($wpdb->collate))
164 339 $charset_collate .= " COLLATE $wpdb->collate";
@@ -174,17 +349,19 @@
174 349 `first_bc_custom_css` TEXT DEFAULT NULL,
175 350 `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE,
176 351 `second_bc_widget_id` VARCHAR(255) DEFAULT NULL,
177 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,
178 355 PRIMARY KEY (`id`)
179 356 )" . $charset_collate . ";";
180 357
181 - //check if table exists
182 - if ($wpdb->get_var("show tables like '" . $settings_table . "'") != $settings_table) {
358 + // create/update the table
183 359 dbDelta($sql_table_settings);
184 - }
185 360 }
186 361 }
187 362 }
188 363
189 364 global $taboolaWP;
190 -$taboolaWP = new TaboolaWP();
365 +$taboolaWP = new TaboolaWP();
366 +
367 +//