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