| @@ -1,20 +1,25 @@ | ||
| 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.7 | |
| 6 | + * Version: 2.0.1 | |
| 7 | 7 | * Author: Taboola |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -define ("XPATH_MARKER","/"); | |
| 11 | -define ("JS_INDICATOR","{JS}"); | |
| 12 | -define ("JS_MARKER","{"); | |
| 10 | +define ("TABOOLA_PLUGIN_VERSION","2.0.1"); // => UPDATE FOR *EVERY* RELEASE (USED FOR TRACKING) | |
| 11 | +define ("TABOOLA_MIN_VER","2.0.1"); // => UPDATE *ONLY* IF THIS RELEASE HAS *DB CHANGES* | |
| 12 | +define ("TABOOLA_DEBUG_MODE", false); // => SET TO 'FALSE' FOR *EVERY* RELEASE (USED TO SUPRESS DEBUGGING LOGS) | |
| 13 | + | |
| 14 | +define ("TABOOLA_OPTION_NAME","taboola_plugin_version"); // *If* this release has DB changes, then the min version will be saved under 'taboola_plugin_version' in 'wp_options'. | |
| 15 | + | |
| 16 | +define ("TABOOLA_XPATH_MARKER","/"); | |
| 17 | +define ("TABOOLA_JS_INDICATOR","{JS}"); | |
| 18 | +define ("TABOOLA_JS_MARKER","{"); | |
| 13 | 19 | define ("TABOOLA_CONTENT_FORMAT_STRING",'string'); |
| 14 | 20 | define ("TABOOLA_CONTENT_FORMAT_SCRIPT",'script'); |
| 15 | 21 | define ("TABOOLA_CONTENT_FORMAT_HTML",'html'); |
| 16 | -define ("TABOOLA_PLUGIN_VERSION","1.0.8"); | |
| 17 | 22 | |
| 18 | 23 | |
| 19 | 24 | include_once('widget.php'); |
| 20 | 25 | require_once('JavaScriptWrapper.php'); |
| @@ -26,10 +31,9 @@ | ||
| 26 | 31 | public $data = array(); |
| 27 | 32 | public $_is_widget_on_page; |
| 28 | 33 | public $_is_head_script_loaded = false; |
| 29 | 34 | |
| 30 | - | |
| 31 | - function TaboolaWP() | |
| 35 | + function __construct() | |
| 32 | 36 | { |
| 33 | 37 | global $wpdb; |
| 34 | 38 | |
| 35 | 39 | //initialize plugin constant |
| @@ -45,36 +49,36 @@ | ||
| 45 | 49 | |
| 46 | 50 | $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings'; |
| 47 | 51 | |
| 48 | 52 | //activation function |
| 49 | - register_activation_hook($this->plugin_name, array(&$this, 'activate')); | |
| 53 | + register_activation_hook($this->plugin_name, array(&$this, 'update_db')); | |
| 54 | + add_action('admin_init', array(&$this, 'update_db')); | |
| 50 | 55 | |
| 51 | 56 | // Enable sidebar widgets |
| 52 | - if($this->settings != NULL && !empty($this->settings->publisher_id)){ | |
| 57 | + if ($this->settings != NULL && !empty($this->settings->publisher_id)){ | |
| 53 | 58 | //register Taboola widget |
| 54 | 59 | add_action('widgets_init', |
| 55 | - create_function('', 'return register_widget("WP_Widget_Taboola");') | |
| 60 | + function(){ | |
| 61 | + return register_widget("WP_Widget_Taboola"); | |
| 62 | + } | |
| 56 | 63 | ); |
| 57 | 64 | } |
| 58 | 65 | |
| 59 | - $this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled; | |
| 66 | + //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled; | |
| 60 | 67 | |
| 61 | 68 | if (is_admin()) { |
| 62 | 69 | //add menu for plugin |
| 63 | 70 | add_action( 'admin_menu', array(&$this, 'admin_generate_menu') ); |
| 64 | 71 | add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 ); |
| 65 | - } else { | |
| 66 | - if($this->settings != NULL){ | |
| 72 | + }else if ($this->settings != NULL){ | |
| 67 | 73 | add_action('wp_head', array(&$this, 'taboola_header_loader_inject')); |
| 68 | 74 | add_action('wp_footer', array(&$this, 'taboola_footer_loader_js')); |
| 69 | 75 | add_filter('the_content', array(&$this, 'load_taboola_content')); |
| 70 | - } | |
| 76 | + add_filter('the_content', array(&$this, 'load_taboola_content_mid')); | |
| 77 | + add_filter('the_content', array(&$this, 'load_taboola_content_home')); | |
| 71 | 78 | } |
| 72 | - } | |
| 79 | + } | |
| 73 | 80 | |
| 74 | - | |
| 75 | - | |
| 76 | - | |
| 77 | 81 | function plugin_action_links($links, $file) { |
| 78 | 82 | static $this_plugin; |
| 79 | 83 | |
| 80 | 84 | if (!$this_plugin) { |
| @@ -93,8 +97,24 @@ | ||
| 93 | 97 | $retVal = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->first_bc_enabled && trim($this->settings->first_bc_widget_id) != ''); |
| 94 | 98 | return $retVal; |
| 95 | 99 | } |
| 96 | 100 | |
| 101 | + private function should_show_content_widget_mid(){ | |
| 102 | + // PC - only if v2 was installed: | |
| 103 | + if (!$this->is_db_updated_for_min_ver("2.0.0")) | |
| 104 | + return false; | |
| 105 | + $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->mid_enabled && trim($this->settings->mid_widget_id) != ''); | |
| 106 | + return $retVal1; | |
| 107 | + } | |
| 108 | + | |
| 109 | + private function should_show_content_widget_home(){ | |
| 110 | + // PC - only if v2 was installed: | |
| 111 | + if (!$this->is_db_updated_for_min_ver("2.0.0")) | |
| 112 | + return false; | |
| 113 | + $retVal2 = ((trim($this->settings->publisher_id) != '') && is_front_page() && $this->settings->home_enabled && trim($this->settings->home_widget_id) != ''); | |
| 114 | + return $retVal2; | |
| 115 | + } | |
| 116 | + | |
| 97 | 117 | private function should_show_sidebar_widget(){ |
| 98 | 118 | $retVal = ((trim($this->settings->publisher_id) != '') && is_active_widget( false, false, TABOOLA_WIDGET_BASE_ID, true )); |
| 99 | 119 | return $retVal; |
| 100 | 120 | } |
| @@ -100,16 +120,15 @@ | ||
| 100 | 120 | } |
| 101 | 121 | |
| 102 | 122 | // Determine if a taboola widget should be added somewhere on the current page (content or sidebar) |
| 103 | 123 | function is_widget_on_page(){ |
| 104 | - return $this->should_show_content_widget() || $this->should_show_sidebar_widget(); | |
| 124 | + return $this->should_show_content_widget() || $this->should_show_content_widget_mid() || $this->should_show_content_widget_home() || $this->should_show_sidebar_widget(); | |
| 105 | 125 | } |
| 106 | 126 | |
| 107 | 127 | function get_page_type(){ |
| 108 | - | |
| 109 | 128 | $page_type='article'; |
| 110 | 129 | if (is_front_page()){ |
| 111 | - $page_type='home'; | |
| 130 | + $page_type='home'; | |
| 112 | 131 | }else if (is_category() || is_archive() || is_search()){ |
| 113 | 132 | $page_type='category'; |
| 114 | 133 | } |
| 115 | 134 | return $page_type; |
| @@ -125,13 +144,16 @@ | ||
| 125 | 144 | $stringParams = array( |
| 126 | 145 | '{{PUBLISHER_ID}}' => $this->settings->publisher_id, |
| 127 | 146 | '{{PAGE_TYPE}}' => $this->get_page_type(), |
| 128 | 147 | '{{WORDPRESS_VERSION}}' => get_bloginfo('version'), |
| 129 | - '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION | |
| 148 | + '{{PHP_VERSION}}' => phpversion(), | |
| 149 | + '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION, | |
| 150 | + '{{LOC_MID}}' => $this->settings->mid_location_string, | |
| 151 | + '{{LOC_HOME}}' => $this->settings->home_location_string | |
| 130 | 152 | ); |
| 131 | 153 | |
| 132 | 154 | $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams); |
| 133 | - $head_string = $scriptWrapper->getScripMarkupString(); | |
| 155 | + $head_string = $scriptWrapper->getScriptMarkupString(); | |
| 134 | 156 | } |
| 135 | 157 | |
| 136 | 158 | return $head_string; |
| 137 | 159 | } |
| @@ -138,9 +160,8 @@ | ||
| 138 | 160 | |
| 139 | 161 | |
| 140 | 162 | // This function is used for the hook action, injects the header content to the <head> tag. |
| 141 | 163 | function taboola_header_loader_inject(){ |
| 142 | - | |
| 143 | 164 | echo $this->taboola_header_loader_js(); |
| 144 | 165 | } |
| 145 | 166 | |
| 146 | 167 | |
| @@ -148,43 +169,77 @@ | ||
| 148 | 169 | |
| 149 | 170 | // Only adding flush script if a widget is going to be placed on the page. |
| 150 | 171 | if ( $this->is_widget_on_page() ){ |
| 151 | 172 | $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array()); |
| 152 | - echo $flushInjectionScript->getScripMarkupString(); | |
| 173 | + echo $flushInjectionScript->getScriptMarkupString(); | |
| 153 | 174 | } |
| 154 | 175 | } |
| 155 | 176 | |
| 177 | + // Below-article widget | |
| 156 | 178 | function load_taboola_content($content) |
| 157 | 179 | { |
| 158 | 180 | $taboola_content = array(); |
| 159 | 181 | if ($this->should_show_content_widget()){ |
| 160 | 182 | |
| 161 | - $firstWidgetParams = array('{{WIDGET_ID}}' => $this->settings->first_bc_widget_id, | |
| 162 | - '{{CONTAINER}}' => 'taboola-below-article', | |
| 163 | - '{{PLACEMENT}}' => 'below-article'); | |
| 183 | + $firstWidgetParams = array( | |
| 184 | + '{{WIDGET_ID}}' => $this->settings->first_bc_widget_id, | |
| 185 | + '{{CONTAINER}}' => 'taboola-below-article-thumbnails', | |
| 186 | + // PC - If v2 was not installed, then use v1 logic | |
| 187 | + '{{PLACEMENT}}' => (!empty($this->settings->first_bc_placement)) ? $this->settings->first_bc_placement : 'below-article' // In case v1 upgrade has not run yet | |
| 188 | + ); | |
| 164 | 189 | |
| 165 | 190 | $firstWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$firstWidgetParams); |
| 166 | - $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article'></div>"; | |
| 191 | + $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article-thumbnails'></div>"; | |
| 167 | 192 | $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $firstWidgetScript; |
| 168 | 193 | |
| 169 | - // Adding the 2nd widget if needed | |
| 170 | - if($this->settings->second_bc_enabled && trim($this->settings->second_bc_widget_id) != ''){ | |
| 194 | + $content = $this->embed_taboola_content_location($content,$taboola_content); | |
| 195 | + } | |
| 171 | 196 | |
| 172 | - $secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->second_bc_widget_id, | |
| 173 | - '{{CONTAINER}}' => 'taboola-below-article-second', | |
| 174 | - '{{PLACEMENT}}' => 'below-article-2nd'); | |
| 197 | + return $content; | |
| 198 | + } | |
| 199 | + | |
| 200 | + // Mid-article-widget | |
| 201 | + function load_taboola_content_mid($content) | |
| 202 | + { | |
| 203 | + $taboola_content_mid = array(); | |
| 204 | + if ($this->should_show_content_widget_mid()){ | |
| 205 | + | |
| 206 | + $secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->mid_widget_id, | |
| 207 | + '{{CONTAINER}}' => 'taboola-mid-article-thumbnails', | |
| 208 | + '{{PLACEMENT}}' => $this->settings->mid_placement); | |
| 209 | + | |
| 175 | 210 | $secondWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$secondWidgetParams); |
| 211 | + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-mid-article-thumbnails'></div>"; | |
| 212 | + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $secondWidgetScript; | |
| 176 | 213 | |
| 177 | - $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article-second'></div>"; | |
| 178 | - $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $secondWidgetScript; | |
| 179 | - } | |
| 214 | + $content = $this->embed_taboola_content_location_mid($content,$taboola_content_mid,trim($this->settings->mid_location_string)); | |
| 215 | + } | |
| 180 | 216 | |
| 181 | - $content = $this->embed_taboola_content_location($content,$taboola_content,trim($this->settings->location_string)); | |
| 182 | - } | |
| 217 | + return $content; | |
| 218 | + } | |
| 183 | 219 | |
| 220 | + // Homepage widget | |
| 221 | + function load_taboola_content_home($content) | |
| 222 | + { | |
| 223 | + | |
| 224 | + $taboola_content_home = array(); | |
| 225 | + if ($this->should_show_content_widget_home()){ | |
| 226 | + | |
| 227 | + $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id, | |
| 228 | + '{{CONTAINER}}' => 'taboola-homepage-thumbnails', | |
| 229 | + '{{PLACEMENT}}' => $this->settings->home_placement); | |
| 230 | + | |
| 231 | + $homeWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$homeWidgetParams); | |
| 232 | + $taboola_content_home[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-homepage-thumbnails'></div>"; | |
| 233 | + $taboola_content_home[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $homeWidgetScript; | |
| 234 | + | |
| 235 | + $content = $this->embed_taboola_content_location_home($content,$taboola_content_home,trim($this->settings->home_location_string)); | |
| 236 | + } | |
| 184 | 237 | return $content; |
| 238 | + | |
| 185 | 239 | } |
| 186 | 240 | |
| 241 | + // Below-article widget | |
| 187 | 242 | |
| 188 | 243 | // Extract the taboola content in the required format: |
| 189 | 244 | // String - for injecting on the servr side |
| 190 | 245 | // Script or HTML - for injecting on the client side |
| @@ -207,26 +262,95 @@ | ||
| 207 | 262 | return $ret_val; |
| 208 | 263 | } |
| 209 | 264 | |
| 210 | 265 | |
| 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){ | |
| 266 | + // Mid-article widget | |
| 267 | + function format_taboola_content_mid($taboola_content_mid,$format){ | |
| 268 | + $ret_val = null; | |
| 269 | + | |
| 270 | + switch($format){ | |
| 271 | + case TABOOLA_CONTENT_FORMAT_STRING: | |
| 272 | + $result_string = join("",$taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML]). | |
| 273 | + "<script type='text/javascript'>".join("\n",$taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>"; | |
| 274 | + $ret_val = $result_string; | |
| 275 | + break; | |
| 276 | + | |
| 277 | + // script or html | |
| 278 | + default: | |
| 279 | + $ret_val = str_replace("\n","",join("",$taboola_content_mid[$format])); | |
| 280 | + break; | |
| 281 | + } | |
| 282 | + | |
| 283 | + return $ret_val; | |
| 284 | + } | |
| 285 | + | |
| 286 | + | |
| 287 | + // Homepage widget | |
| 288 | + function format_taboola_content_home($taboola_content_home,$format){ | |
| 289 | + | |
| 290 | + $ret_val = null; | |
| 291 | + | |
| 292 | + switch($format){ | |
| 293 | + case TABOOLA_CONTENT_FORMAT_STRING: | |
| 294 | + $result_string = join("",$taboola_content_home[TABOOLA_CONTENT_FORMAT_HTML]). | |
| 295 | + "<script type='text/javascript'>".join("\n",$taboola_content_home[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>"; | |
| 296 | + $ret_val = $result_string; | |
| 297 | + break; | |
| 298 | + | |
| 299 | + // script or html | |
| 300 | + default: | |
| 301 | + $ret_val = str_replace("\n","",join("",$taboola_content_home[$format])); | |
| 302 | + break; | |
| 303 | + } | |
| 304 | + | |
| 305 | + return $ret_val; | |
| 306 | + | |
| 307 | + } | |
| 308 | + | |
| 309 | + // Below-article widget | |
| 310 | + // Do the actual logic of choosing where to place the taboola content. | |
| 311 | + function embed_taboola_content_location($content, $taboola_content){ | |
| 213 | 312 | $do_default = true; |
| 214 | 313 | |
| 314 | + // tag is placed outside of content in order to allow "read more" functionality. | |
| 315 | + if ($this->settings->out_of_content_enabled){ | |
| 316 | + | |
| 317 | + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( | |
| 318 | + "{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML), | |
| 319 | + "{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT)) | |
| 320 | + ); | |
| 321 | + $scriptWrapper->appendScript("injectWidgetByMarker('tbmarker');"); | |
| 322 | + $content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$scriptWrapper."</script>"; | |
| 323 | + $do_default = false; | |
| 324 | + } | |
| 325 | + | |
| 326 | + // Default for below-article widget - add to the end of the content | |
| 327 | + if ($do_default){ | |
| 328 | + $content = $content.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING); | |
| 329 | + } | |
| 330 | + | |
| 331 | + return $content; | |
| 332 | + } | |
| 333 | + | |
| 334 | + // Mid-article widget | |
| 335 | + // Do the actual logic of choosing where to place the taboola content based on the "location" attribute | |
| 336 | + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location){ | |
| 337 | + $do_default = true; | |
| 338 | + | |
| 215 | 339 | if (isset($location) && $location != ''){ |
| 216 | 340 | $first_char = substr($location,0,1); |
| 217 | 341 | |
| 218 | 342 | // DIV/XPATH provided for JS handling |
| 219 | - if ($first_char == JS_MARKER){ | |
| 220 | - $full_indicator = substr($location,0,strlen(JS_INDICATOR)); | |
| 343 | + if ($first_char == TABOOLA_JS_MARKER){ | |
| 344 | + $full_indicator = substr($location,0,strlen(TABOOLA_JS_INDICATOR)); | |
| 221 | 345 | |
| 222 | - if ($full_indicator == JS_INDICATOR){ | |
| 346 | + if ($full_indicator == TABOOLA_JS_INDICATOR){ | |
| 223 | 347 | |
| 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 | - ); | |
| 348 | + $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR)); | |
| 349 | + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( | |
| 350 | + "{{HTML}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_HTML), | |
| 351 | + "{{SCRIPT}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_SCRIPT)) | |
| 352 | + ); | |
| 229 | 353 | $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');"); |
| 230 | 354 | $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>"; |
| 231 | 355 | |
| 232 | 356 | $do_default = false; |
| @@ -234,45 +358,83 @@ | ||
| 234 | 358 | |
| 235 | 359 | // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm) |
| 236 | 360 | // basically it's CSS selectors like in jQuery |
| 237 | 361 | } else{ |
| 238 | - | |
| 239 | 362 | require_once('simple_html_dom.php'); |
| 240 | 363 | |
| 241 | 364 | $html_doc = str_get_html($content); |
| 242 | - $target_location = $html_doc->find($location,0); | |
| 365 | + $target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1); | |
| 243 | 366 | |
| 244 | 367 | // if the location was found within the html content |
| 245 | 368 | if (isset($target_location) && is_object($target_location)){ |
| 246 | 369 | |
| 247 | 370 | // adding taboola content AFTER the target location |
| 248 | - $target_location->outertext = $target_location->outertext.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING); | |
| 371 | + $target_location->outertext = $target_location->outertext.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING); | |
| 249 | 372 | $content = $html_doc; |
| 250 | 373 | $do_default = false; |
| 251 | 374 | } |
| 252 | 375 | } |
| 376 | + } | |
| 377 | + // Default for below-article widget - add to the end of the content | |
| 378 | + // if ($do_default){ | |
| 379 | + // $content = $content.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING); | |
| 380 | + // } | |
| 253 | 381 | |
| 254 | - // tag is placed outside of content in order to allow "read more" functionality. | |
| 255 | - }elseif ($this->should_place_tag_outside_of_content){ | |
| 382 | + return $content; | |
| 383 | + } | |
| 256 | 384 | |
| 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 | - } | |
| 385 | + // Homepage widget | |
| 386 | + // Do the actual logic of choosing where to place the taboola content based on the "location" attribute | |
| 387 | + function embed_taboola_content_location_home($content, $taboola_content_home, $location){ | |
| 265 | 388 | |
| 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 | - } | |
| 389 | + $do_default = true; | |
| 270 | 390 | |
| 271 | - return $content; | |
| 391 | + if (isset($location) && $location != ''){ | |
| 392 | + $first_char = substr($location,0,1); | |
| 393 | + | |
| 394 | + // DIV/XPATH provided for JS handling | |
| 395 | + if ($first_char == TABOOLA_JS_MARKER){ | |
| 396 | + $full_indicator = substr($location,0,strlen(TABOOLA_JS_INDICATOR)); | |
| 397 | + | |
| 398 | + if ($full_indicator == TABOOLA_JS_INDICATOR){ | |
| 399 | + | |
| 400 | + $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR)); | |
| 401 | + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( | |
| 402 | + "{{HTML}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_HTML), | |
| 403 | + "{{SCRIPT}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_SCRIPT)) | |
| 404 | + ); | |
| 405 | + $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');"); | |
| 406 | + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>"; | |
| 407 | + | |
| 408 | + $do_default = false; | |
| 409 | + } | |
| 410 | + | |
| 411 | + // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm) | |
| 412 | + // basically it's CSS selectors like in jQuery | |
| 413 | + } else{ | |
| 414 | + require_once('simple_html_dom.php'); | |
| 415 | + | |
| 416 | + $html_doc = str_get_html($content); | |
| 417 | + $target_location = $html_doc->find($location,($this->settings->home_location_string_occurrence)-1); | |
| 418 | + | |
| 419 | + // if the location was found within the html content | |
| 420 | + if (isset($target_location) && is_object($target_location)){ | |
| 421 | + | |
| 422 | + // adding taboola content AFTER the target location | |
| 423 | + $target_location->outertext = $target_location->outertext.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING); | |
| 424 | + $content = $html_doc; | |
| 425 | + $do_default = false; | |
| 426 | + } | |
| 427 | + } | |
| 428 | + } | |
| 429 | + // Default for below-article widget - add to the end of the content | |
| 430 | + // if ($do_default){ | |
| 431 | + // $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING); | |
| 432 | + // } | |
| 433 | + return $content; | |
| 434 | + | |
| 272 | 435 | } |
| 273 | 436 | |
| 274 | - | |
| 275 | 437 | function admin_generate_menu(){ |
| 276 | 438 | global $current_user; |
| 277 | 439 | add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110); |
| 278 | 440 | } |
| @@ -283,28 +445,101 @@ | ||
| 283 | 445 | $taboola_errors = array(); |
| 284 | 446 | if($_SERVER['REQUEST_METHOD'] == 'POST'){ |
| 285 | 447 | |
| 286 | 448 | if(trim($_POST['publisher_id']) == ''){ |
| 287 | - $taboola_errors[] = "Please add a 'Publisher ID' in order to apply changes to your widgets"; | |
| 449 | + $taboola_errors[] = "Publisher ID"; | |
| 288 | 450 | } |
| 289 | - if(($_POST['first_bc_enabled'] == 'on' && trim($_POST['first_bc_widget_id']) == '') || | |
| 290 | - ($_POST['second_bc_enabled'] == 'on' && trim($_POST['second_bc_widget_id']) == '') | |
| 291 | - ){ | |
| 292 | - $taboola_errors[] = "Please add a 'Widget ID' in order to apply changes to your widgets"; | |
| 451 | + | |
| 452 | + if(isset($_POST['first_bc_enabled'])) { | |
| 453 | + if (trim($_POST['first_bc_widget_id']) == '') { | |
| 454 | + $taboola_errors[] = "Below-article > Widget ID"; | |
| 455 | + } | |
| 456 | + if (trim($_POST['first_bc_placement']) == '') { | |
| 457 | + $taboola_errors[] = "Below-article > Placement Name"; | |
| 458 | + } | |
| 293 | 459 | } |
| 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"; | |
| 460 | + | |
| 461 | + if(isset($_POST['mid_enabled'])) { | |
| 462 | + if (trim($_POST['mid_widget_id']) == '') { | |
| 463 | + $taboola_errors[] = "Mid-article > Widget ID"; | |
| 464 | + } | |
| 465 | + if (trim($_POST['mid_placement']) == '') { | |
| 466 | + $taboola_errors[] = "Mid-article > Placement Name"; | |
| 467 | + } | |
| 468 | + if (trim($_POST['mid_location_string']) == '') { | |
| 469 | + $taboola_errors[] = "Mid-article > CSS selector"; | |
| 470 | + } | |
| 471 | + else { | |
| 472 | + // If the 'location' WAS filled in, then... | |
| 473 | + | |
| 474 | + // 1) Check for a valid 'location': | |
| 475 | + | |
| 476 | + // Validation method has not been implemented | |
| 477 | + // mid_if (!$this->is_locaton_string_valid1($_POST['mid_location_string'])) { | |
| 478 | + // $taboola_errors[] = "Mid-article > CSS Selector (invalid value)"; | |
| 479 | + // } | |
| 480 | + | |
| 481 | + // 2) Validate the 'occurrence': | |
| 482 | + if (!$this->is_mid_location_string_occurrence_valid($_POST['mid_location_string_occurrence'])) { | |
| 483 | + $taboola_errors[] = "Mid-article > Occurance (must be >= 1)"; | |
| 484 | + } | |
| 485 | + } | |
| 486 | + | |
| 296 | 487 | } |
| 488 | + | |
| 489 | + if(isset($_POST['home_enabled'])) { | |
| 490 | + if (trim($_POST['home_widget_id']) == '') { | |
| 491 | + $taboola_errors[] = "Homepage > Widget ID"; | |
| 492 | + } | |
| 493 | + if (trim($_POST['home_placement']) == '') { | |
| 494 | + $taboola_errors[] = "Homepage > Placement Name"; | |
| 495 | + } | |
| 496 | + | |
| 497 | + if (trim($_POST['home_location_string']) == '') { | |
| 498 | + $taboola_errors[] = "Homepage > CSS selector"; | |
| 499 | + } | |
| 500 | + else { | |
| 501 | + // If the 'location' WAS filled in, then... | |
| 502 | + | |
| 503 | + // 1) Check for a valid 'location': | |
| 504 | + | |
| 505 | + // Validation method has not been implemented | |
| 506 | + // if (!$this->is_home_location_string_valid($_POST['home_location_string'])) { | |
| 507 | + // $taboola_errors[] = "Homepage > CSS Selector (invalid value)"; | |
| 508 | + // } | |
| 509 | + | |
| 510 | + // 2) Validate the 'occurrence': | |
| 511 | + if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) { | |
| 512 | + $taboola_errors[] = "Homepage > Occurance (must be >= 1)"; | |
| 513 | + } | |
| 514 | + } | |
| 515 | + } | |
| 516 | + | |
| 297 | 517 | if(count($taboola_errors) == 0){ |
| 298 | 518 | |
| 299 | 519 | $data = array( |
| 300 | 520 | "publisher_id" => trim($_POST['publisher_id']), |
| 301 | - "first_bc_enabled" => $_POST['first_bc_enabled'] == 'on' ? true : false, | |
| 302 | - "first_bc_widget_id" => trim($_POST['first_bc_widget_id']), | |
| 303 | - "second_bc_enabled" => $_POST['second_bc_enabled'] == 'on' ? true : false, | |
| 304 | - "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 | |
| 521 | + | |
| 522 | + "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false, | |
| 523 | + "first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '', | |
| 524 | + "first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '', | |
| 525 | + | |
| 526 | + "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false, | |
| 527 | + | |
| 528 | + "mid_enabled" => isset($_POST['mid_enabled']) ? true : false, | |
| 529 | + "mid_widget_id" => !empty($_POST['mid_widget_id']) ? trim($_POST['mid_widget_id']) : '', | |
| 530 | + "mid_placement" => !empty($_POST['mid_placement']) ? trim($_POST['mid_placement']) : '', | |
| 531 | + "mid_paragraph_ui_mode" => !empty($_POST['mid_paragraph_ui_mode']) ? trim($_POST['mid_paragraph_ui_mode']) : '', | |
| 532 | + | |
| 533 | + "mid_location_string_occurrence" => !empty($_POST['mid_location_string_occurrence']) ? $_POST['mid_location_string_occurrence'] : '', | |
| 534 | + "mid_location_string" => !empty($_POST['mid_location_string']) ? trim($_POST['mid_location_string']) : '', | |
| 535 | + | |
| 536 | + "home_enabled" => isset($_POST['home_enabled']) ? true : false, | |
| 537 | + "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '', | |
| 538 | + "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '', | |
| 539 | + | |
| 540 | + "home_location_string_occurrence" => !empty($_POST['home_location_string_occurrence']) ? $_POST['home_location_string_occurrence'] : '', | |
| 541 | + "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : '' | |
| 307 | 542 | ); |
| 308 | 543 | |
| 309 | 544 | //var_dump($settings); |
| 310 | 545 | if($settings == NULL){ |
| @@ -315,23 +550,138 @@ | ||
| 315 | 550 | } |
| 316 | 551 | $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 317 | 552 | } |
| 318 | 553 | |
| 554 | + include_once('settings.php'); | |
| 555 | + } | |
| 319 | 556 | |
| 320 | - include_once('settings.php'); | |
| 557 | + function is_mid_location_string_valid($mid_location_string){ | |
| 558 | + // TODO:: validate the location string | |
| 559 | + return true; | |
| 321 | 560 | } |
| 322 | 561 | |
| 323 | - function is_location_string_valid($location_string){ | |
| 562 | + function is_mid_location_string_occurrence_valid($mid_location_string_occurrence){ | |
| 563 | + | |
| 564 | + if ((int)$mid_location_string_occurrence >= 1) { | |
| 565 | + return true; | |
| 566 | + } | |
| 567 | + return false; | |
| 568 | + } | |
| 569 | + | |
| 570 | + function is_home_location_string_valid($home_location_string){ | |
| 324 | 571 | // TODO:: validate the location string |
| 325 | 572 | return true; |
| 326 | 573 | } |
| 327 | - function activate(){ | |
| 574 | + | |
| 575 | + function is_home_location_string_occurrence_valid($home_location_string_occurrence){ | |
| 576 | + | |
| 577 | + if ((int)$home_location_string_occurrence >= 1) { | |
| 578 | + return true; | |
| 579 | + } | |
| 580 | + return false; | |
| 581 | + } | |
| 582 | + function columnExists($column_name) { | |
| 328 | 583 | global $wpdb; |
| 584 | + $table_name = $wpdb->prefix . "_taboola_settings"; | |
| 585 | + tb_write_log("table name : " . $table_name); | |
| 586 | + tb_write_log("column name : " . $column_name); | |
| 329 | 587 | |
| 588 | + $column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE '$column_name'") !== null; | |
| 589 | + | |
| 590 | + if ($column_exists) { | |
| 591 | + tb_write_log("Yes - column " . $column_name . " DOES exist!"); //PC | |
| 592 | + return true; | |
| 593 | + } else { | |
| 594 | + tb_write_log("No - column " . $column_name . " does NOT exist!"); //PC | |
| 595 | + return false; | |
| 596 | + } | |
| 597 | + } | |
| 598 | + | |
| 599 | + function is_upgrade_from_v1() { | |
| 600 | + | |
| 601 | + if($this->columnExists("first_bc_widget_id") && !$this->columnExists("first_bc_placement")){ | |
| 602 | + tb_write_log("This IS a v1 upgrade!"); //PC | |
| 603 | + return true; | |
| 604 | + } | |
| 605 | + else { | |
| 606 | + tb_write_log("This is NOT a v1 upgrade!"); //PC | |
| 607 | + return false; | |
| 608 | + } | |
| 609 | + | |
| 610 | + } | |
| 611 | + | |
| 612 | + function is_db_updated_for_min_ver($min_ver) { | |
| 613 | + /** | |
| 614 | + * Checks if the DB was *previously* updated for the minimum version specified. | |
| 615 | + * $min_ver should be passed in a format like this: "2.1.0" | |
| 616 | + */ | |
| 617 | + global $wpdb; | |
| 618 | + | |
| 619 | + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found. | |
| 620 | + $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0'); | |
| 621 | + | |
| 622 | + $db_is_up_to_date = version_compare($saved_version, $min_ver, '>='); | |
| 623 | + //tb_write_log($db_is_up_to_date); | |
| 624 | + return $db_is_up_to_date; | |
| 625 | + | |
| 626 | + } | |
| 627 | + | |
| 628 | + function is_db_updated_for_current_ver() { | |
| 629 | + /** | |
| 630 | + * Checks if the DB was *previously* updated for the version currently loaded. | |
| 631 | + */ | |
| 632 | + global $wpdb; | |
| 633 | + | |
| 634 | + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found. | |
| 635 | + $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0'); | |
| 636 | + | |
| 637 | + // Get the currently loaded plugin version from wp_options. | |
| 638 | + $plugin_version = $this->get_loaded_plugin_version(); | |
| 639 | + | |
| 640 | + $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>='); | |
| 641 | + //tb_write_log($db_is_up_to_date); | |
| 642 | + return $db_is_up_to_date; | |
| 643 | + | |
| 644 | + } | |
| 645 | + | |
| 646 | + function get_loaded_plugin_version() { | |
| 647 | + | |
| 648 | + $plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only | |
| 649 | + $loaded_plugin_version = $plugin_data['Version']; | |
| 650 | + return $loaded_plugin_version; | |
| 651 | + | |
| 652 | + } | |
| 653 | + | |
| 654 | + function save_taboola_version($min_ver) { | |
| 655 | + // Get the currently loaded plugin version | |
| 656 | + // $plugin_version = $this->get_loaded_plugin_version(); | |
| 657 | + | |
| 658 | + tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC | |
| 659 | + | |
| 660 | + // Check the saved version in wp_options. Default to '' if the option is not found. | |
| 661 | + $saved_version = get_option(TABOOLA_OPTION_NAME, ''); | |
| 662 | + if ($saved_version == '') { | |
| 663 | + add_option(TABOOLA_OPTION_NAME, $min_ver); | |
| 664 | + } | |
| 665 | + else { | |
| 666 | + update_option(TABOOLA_OPTION_NAME, $min_ver); | |
| 667 | + } | |
| 668 | + } | |
| 669 | + | |
| 670 | + function update_db(){ | |
| 671 | + | |
| 672 | + // If we are up to date, then skip this method... | |
| 673 | + if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) { | |
| 674 | + //tb_write_log("All up to date!"); | |
| 675 | + return; | |
| 676 | + } | |
| 677 | + | |
| 678 | + $this->save_taboola_version(TABOOLA_MIN_VER); | |
| 679 | + | |
| 680 | + global $wpdb; | |
| 330 | 681 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 682 | + | |
| 331 | 683 | |
| 332 | - $settings_table = $this->tbl_taboola_settings; | |
| 333 | - | |
| 334 | 684 | //check mysql version |
| 335 | 685 | if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) { |
| 336 | 686 | if (!empty($wpdb->charset)) |
| 337 | 687 | $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; |
| @@ -338,8 +688,14 @@ | ||
| 338 | 688 | if (!empty($wpdb->collate)) |
| 339 | 689 | $charset_collate .= " COLLATE $wpdb->collate"; |
| 340 | 690 | } |
| 341 | 691 | |
| 692 | + // Check if this is an upgrade from v1 | |
| 693 | + $is_upgrade_from_v1 = $this->is_upgrade_from_v1(); | |
| 694 | + | |
| 695 | + // tb_write_log("Is upgrade from v1: " . ($is_upgrade_from_v1 ? 'true' : 'false')); | |
| 696 | + | |
| 697 | + | |
| 342 | 698 | //settings table structure |
| 343 | 699 | $sql_table_settings = " |
| 344 | 700 | CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` ( |
| 345 | 701 | `id` INT NOT NULL AUTO_INCREMENT , |
| @@ -345,16 +701,30 @@ | ||
| 345 | 701 | `id` INT NOT NULL AUTO_INCREMENT , |
| 346 | 702 | `publisher_id` VARCHAR(255) DEFAULT NULL, |
| 347 | 703 | `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 348 | 704 | `first_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 705 | + `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .", | |
| 349 | 706 | `first_bc_custom_css` TEXT DEFAULT NULL, |
| 350 | 707 | `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 351 | 708 | `second_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 352 | 709 | `second_bc_custom_css` TEXT DEFAULT NULL, |
| 353 | 710 | `location_string` TEXT DEFAULT NULL, |
| 711 | + `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, | |
| 712 | + `mid_widget_id` VARCHAR(255) DEFAULT NULL, | |
| 713 | + `mid_placement` VARCHAR(255) DEFAULT NULL, | |
| 354 | 714 | `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE, |
| 715 | + `mid_location_string` TEXT DEFAULT NULL, | |
| 716 | + `mid_location_string_occurrence` SMALLINT DEFAULT NULL, | |
| 717 | + `mid_paragraph_ui_mode` VARCHAR(255) DEFAULT NULL, | |
| 718 | + `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, | |
| 719 | + `home_widget_id` VARCHAR(255) DEFAULT NULL, | |
| 720 | + `home_placement` VARCHAR(255) DEFAULT NULL, | |
| 721 | + `home_location_string` TEXT DEFAULT NULL, | |
| 722 | + `home_location_string_occurrence` SMALLINT DEFAULT NULL, | |
| 355 | 723 | PRIMARY KEY (`id`) |
| 356 | 724 | )" . $charset_collate . ";"; |
| 725 | + | |
| 726 | + // tb_write_log($sql_table_settings); | |
| 357 | 727 | |
| 358 | 728 | // create/update the table |
| 359 | 729 | dbDelta($sql_table_settings); |
| 360 | 730 | } |
| @@ -362,6 +732,52 @@ | ||
| 362 | 732 | } |
| 363 | 733 | |
| 364 | 734 | global $taboolaWP; |
| 365 | 735 | $taboolaWP = new TaboolaWP(); |
| 736 | + | |
| 737 | +/* | |
| 738 | +A few utility methods for logging | |
| 739 | +*/ | |
| 740 | + | |
| 741 | +// Writes debugging messages to 'logs/debug.log'. | |
| 742 | +// The exact file location depends on *where* you trigger the function. | |
| 743 | +// In this script, we trigger the function from the admin dashboard. | |
| 744 | +// So the log file is located under 'wp-admin/logs'. | |
| 745 | +function tb_write_log($log_msg) | |
| 746 | +{ | |
| 747 | + $log_filename = "logs"; | |
| 748 | + if (!file_exists($log_filename)) | |
| 749 | + { | |
| 750 | + mkdir($log_filename, 0777, true); | |
| 751 | + } | |
| 752 | + $log_file_data = $log_filename.'/debug.log'; | |
| 753 | + | |
| 754 | + // Add date/time | |
| 755 | + $date = date('Y-m-d H:i:s'); | |
| 756 | + $log_msg_with_date = $date." : ".$log_msg; | |
| 757 | + | |
| 758 | + if (TABOOLA_DEBUG_MODE) | |
| 759 | + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND); | |
| 760 | + | |
| 761 | +} | |
| 762 | + | |
| 763 | +// Write to console | |
| 764 | +function tb_console_log( $data ){ | |
| 765 | + | |
| 766 | + If(wp_get_environment_type() === 'development') { | |
| 767 | + | |
| 768 | + echo '<script>'; | |
| 769 | + echo 'console.log('. json_encode( $data ) .')'; | |
| 770 | + echo '</script>'; | |
| 771 | + | |
| 772 | + } | |
| 773 | +} | |
| 774 | + | |
| 775 | +// Write to webpage and stop | |
| 776 | +function tb_print_to_page($data) { | |
| 777 | + If(wp_get_environment_type() === 'development') { | |
| 778 | + print_r($data); | |
| 779 | + die; | |
| 780 | + } | |
| 781 | +} | |
| 366 | 782 | |
| 367 | 783 | // |