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