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