| @@ -1,10 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Plugin Name: Taboola |
| 4 | - * Plugin URI: http://taboola.com | |
| 4 | + * Plugin URI: https://developers.taboola.com/web-integrations/discuss | |
| 5 | 5 | * Description: Taboola |
| 6 | - * Version: 1.0.3 | |
| 6 | + * Version: 1.0.15 | |
| 7 | 7 | * Author: Taboola |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | define ("XPATH_MARKER","/"); |
| @@ -12,11 +12,13 @@ | ||
| 12 | 12 | define ("JS_MARKER","{"); |
| 13 | 13 | define ("TABOOLA_CONTENT_FORMAT_STRING",'string'); |
| 14 | 14 | define ("TABOOLA_CONTENT_FORMAT_SCRIPT",'script'); |
| 15 | 15 | define ("TABOOLA_CONTENT_FORMAT_HTML",'html'); |
| 16 | +define ("TABOOLA_PLUGIN_VERSION","1.0.15"); | |
| 16 | 17 | |
| 17 | 18 | |
| 18 | 19 | include_once('widget.php'); |
| 20 | +require_once('JavaScriptWrapper.php'); | |
| 19 | 21 | |
| 20 | 22 | if (!class_exists('TaboolaWP')) { |
| 21 | 23 | class TaboolaWP |
| 22 | 24 | { |
| @@ -24,9 +26,10 @@ | ||
| 24 | 26 | public $data = array(); |
| 25 | 27 | public $_is_widget_on_page; |
| 26 | 28 | public $_is_head_script_loaded = false; |
| 27 | 29 | |
| 28 | - function TaboolaWP() | |
| 30 | + | |
| 31 | + function __construct() | |
| 29 | 32 | { |
| 30 | 33 | global $wpdb; |
| 31 | 34 | |
| 32 | 35 | //initialize plugin constant |
| @@ -48,12 +51,16 @@ | ||
| 48 | 51 | // Enable sidebar widgets |
| 49 | 52 | if($this->settings != NULL && !empty($this->settings->publisher_id)){ |
| 50 | 53 | //register Taboola widget |
| 51 | 54 | add_action('widgets_init', |
| 52 | - create_function('', 'return register_widget("WP_Widget_Taboola");') | |
| 55 | + function(){ | |
| 56 | + return register_widget("WP_Widget_Taboola"); | |
| 57 | + } | |
| 53 | 58 | ); |
| 54 | 59 | } |
| 55 | 60 | |
| 61 | + //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled; | |
| 62 | + | |
| 56 | 63 | if (is_admin()) { |
| 57 | 64 | //add menu for plugin |
| 58 | 65 | add_action( 'admin_menu', array(&$this, 'admin_generate_menu') ); |
| 59 | 66 | add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 ); |
| @@ -113,15 +120,22 @@ | ||
| 113 | 120 | // return the head loader script |
| 114 | 121 | function taboola_header_loader_js() { |
| 115 | 122 | $head_string = ""; |
| 116 | 123 | |
| 117 | - // Only adding Script1 if a widget is going to be placed on the page. | |
| 124 | + // Only adding the loader if a widget is going to be placed on the page. | |
| 118 | 125 | if ($this->is_widget_on_page()){ |
| 119 | 126 | |
| 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); | |
| 127 | + $stringParams = array( | |
| 128 | + '{{PUBLISHER_ID}}' => $this->settings->publisher_id, | |
| 129 | + '{{PAGE_TYPE}}' => $this->get_page_type(), | |
| 130 | + '{{WORDPRESS_VERSION}}' => get_bloginfo('version'), | |
| 131 | + '{{PHP_VERSION}}' => phpversion(), | |
| 132 | + '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION, | |
| 133 | + '{{LOCATION}}' => $this->settings->location_string | |
| 134 | + ); | |
| 122 | 135 | |
| 123 | - $head_string = '<script type="text/javascript">'.$script1_content.'</script>'; | |
| 136 | + $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams); | |
| 137 | + $head_string = $scriptWrapper->getScripMarkupString(); | |
| 124 | 138 | } |
| 125 | 139 | |
| 126 | 140 | return $head_string; |
| 127 | 141 | } |
| @@ -135,12 +149,12 @@ | ||
| 135 | 149 | |
| 136 | 150 | |
| 137 | 151 | function taboola_footer_loader_js() { |
| 138 | 152 | |
| 139 | - // Only adding Script1 if a widget is going to be placed on the page. | |
| 153 | + // Only adding flush script if a widget is going to be placed on the page. | |
| 140 | 154 | 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>'; | |
| 155 | + $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array()); | |
| 156 | + echo $flushInjectionScript->getScripMarkupString(); | |
| 143 | 157 | } |
| 144 | 158 | } |
| 145 | 159 | |
| 146 | 160 | function load_taboola_content($content) |
| @@ -147,23 +161,26 @@ | ||
| 147 | 161 | { |
| 148 | 162 | $taboola_content = array(); |
| 149 | 163 | if ($this->should_show_content_widget()){ |
| 150 | 164 | |
| 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); | |
| 165 | + $firstWidgetParams = array('{{WIDGET_ID}}' => $this->settings->first_bc_widget_id, | |
| 166 | + '{{CONTAINER}}' => 'taboola-below-article', | |
| 167 | + '{{PLACEMENT}}' => 'below-article'); | |
| 154 | 168 | |
| 155 | - $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = '<div id="taboola-below-article"></div>'; | |
| 156 | - $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $script2_content; | |
| 169 | + $firstWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$firstWidgetParams); | |
| 170 | + $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article'></div>"; | |
| 171 | + $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $firstWidgetScript; | |
| 157 | 172 | |
| 158 | 173 | // Adding the 2nd widget if needed |
| 159 | 174 | 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 | 175 | |
| 164 | - $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = '<div id="taboola-below-article-second"></div>'; | |
| 165 | - $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $script2_content; | |
| 176 | + $secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->second_bc_widget_id, | |
| 177 | + '{{CONTAINER}}' => 'taboola-below-article-second', | |
| 178 | + '{{PLACEMENT}}' => 'below-article-2nd'); | |
| 179 | + $secondWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$secondWidgetParams); | |
| 180 | + | |
| 181 | + $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article-second'></div>"; | |
| 182 | + $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $secondWidgetScript; | |
| 166 | 183 | } |
| 167 | 184 | |
| 168 | 185 | $content = $this->embed_taboola_content_location($content,$taboola_content,trim($this->settings->location_string)); |
| 169 | 186 | } |
| @@ -208,18 +225,19 @@ | ||
| 208 | 225 | |
| 209 | 226 | if ($full_indicator == JS_INDICATOR){ |
| 210 | 227 | |
| 211 | 228 | $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); | |
| 229 | + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( | |
| 230 | + "{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML), | |
| 231 | + "{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT)) | |
| 232 | + ); | |
| 233 | + $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');"); | |
| 234 | + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>"; | |
| 216 | 235 | |
| 217 | - $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$js_inject_script."</script>"; | |
| 218 | 236 | $do_default = false; |
| 219 | 237 | } |
| 220 | 238 | |
| 221 | - // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm) | |
| 239 | + // server side selector provided (see simple_html_dom selectors https://simplehtmldom.sourceforge.net/manual.htm) | |
| 222 | 240 | // basically it's CSS selectors like in jQuery |
| 223 | 241 | } else{ |
| 224 | 242 | |
| 225 | 243 | require_once('simple_html_dom.php'); |
| @@ -235,8 +253,19 @@ | ||
| 235 | 253 | $content = $html_doc; |
| 236 | 254 | $do_default = false; |
| 237 | 255 | } |
| 238 | 256 | } |
| 257 | + | |
| 258 | + // tag is placed outside of content in order to allow "read more" functionality. | |
| 259 | + }elseif ($this->settings->out_of_content_enabled){ | |
| 260 | + | |
| 261 | + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( | |
| 262 | + "{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML), | |
| 263 | + "{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT)) | |
| 264 | + ); | |
| 265 | + $scriptWrapper->appendScript("injectWidgetByMarker('tbmarker');"); | |
| 266 | + $content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$scriptWrapper."</script>"; | |
| 267 | + $do_default = false; | |
| 239 | 268 | } |
| 240 | 269 | |
| 241 | 270 | // Default - add to the end of the content |
| 242 | 271 | if ($do_default){ |
| @@ -260,10 +289,10 @@ | ||
| 260 | 289 | |
| 261 | 290 | if(trim($_POST['publisher_id']) == ''){ |
| 262 | 291 | $taboola_errors[] = "Please add a 'Publisher ID' in order to apply changes to your widgets"; |
| 263 | 292 | } |
| 264 | - if(($_POST['first_bc_enabled'] == 'on' && trim($_POST['first_bc_widget_id']) == '') || | |
| 265 | - ($_POST['second_bc_enabled'] == 'on' && trim($_POST['second_bc_widget_id']) == '') | |
| 293 | + if((isset($_POST['first_bc_enabled']) && trim($_POST['first_bc_widget_id']) == '') || | |
| 294 | + (isset($_POST['second_bc_enabled']) && trim($_POST['second_bc_widget_id']) == '') | |
| 266 | 295 | ){ |
| 267 | 296 | $taboola_errors[] = "Please add a 'Widget ID' in order to apply changes to your widgets"; |
| 268 | 297 | } |
| 269 | 298 | if (trim($_POST['location_string']) != '' && !$this->is_location_string_valid($_POST['location_string'])){ |
| @@ -272,13 +301,14 @@ | ||
| 272 | 301 | if(count($taboola_errors) == 0){ |
| 273 | 302 | |
| 274 | 303 | $data = array( |
| 275 | 304 | "publisher_id" => trim($_POST['publisher_id']), |
| 276 | - "first_bc_enabled" => $_POST['first_bc_enabled'] == 'on' ? true : false, | |
| 305 | + "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false, | |
| 277 | 306 | "first_bc_widget_id" => trim($_POST['first_bc_widget_id']), |
| 278 | - "second_bc_enabled" => $_POST['second_bc_enabled'] == 'on' ? true : false, | |
| 307 | + "second_bc_enabled" => isset($_POST['second_bc_enabled']) ? true : false, | |
| 279 | 308 | "second_bc_widget_id" => trim($_POST['second_bc_widget_id']), |
| 280 | - "location_string" => trim($_POST['location_string']) | |
| 309 | + "location_string" => trim($_POST['location_string']), | |
| 310 | + "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false | |
| 281 | 311 | ); |
| 282 | 312 | |
| 283 | 313 | //var_dump($settings); |
| 284 | 314 | if($settings == NULL){ |
| @@ -305,9 +335,9 @@ | ||
| 305 | 335 | |
| 306 | 336 | $settings_table = $this->tbl_taboola_settings; |
| 307 | 337 | |
| 308 | 338 | //check mysql version |
| 309 | - if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) { | |
| 339 | + if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) { | |
| 310 | 340 | if (!empty($wpdb->charset)) |
| 311 | 341 | $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; |
| 312 | 342 | if (!empty($wpdb->collate)) |
| 313 | 343 | $charset_collate .= " COLLATE $wpdb->collate"; |
| @@ -324,12 +354,13 @@ | ||
| 324 | 354 | `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 325 | 355 | `second_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 326 | 356 | `second_bc_custom_css` TEXT DEFAULT NULL, |
| 327 | 357 | `location_string` TEXT DEFAULT NULL, |
| 358 | + `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE, | |
| 328 | 359 | PRIMARY KEY (`id`) |
| 329 | 360 | )" . $charset_collate . ";"; |
| 330 | 361 | |
| 331 | - // creat/update the table | |
| 362 | + // create/update the table | |
| 332 | 363 | dbDelta($sql_table_settings); |
| 333 | 364 | } |
| 334 | 365 | } |
| 335 | 366 | } |