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