| @@ -1,16 +1,22 @@ | ||
| 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/docs/wordpress-plugin | |
| 5 | 5 | * Description: Taboola |
| 6 | - * Version: 1.0.3 | |
| 6 | + * Version: 2.2.3 | |
| 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.2.3"); // => UPDATE THIS FOR *EVERY* RELEASE (USED FOR TRACKING) | |
| 11 | +define ("TABOOLA_MIN_VER","2.2.2"); // => UPDATE THIS *ONLY* IF THIS RELEASE HAS *DB CHANGES* | |
| 12 | +define ("TABOOLA_DEBUG_MODE", false); // => SET THIS TO 'FALSE' FOR *EVERY* RELEASE (USED TO SUPRESS DEBUGGING LOGS) | |
| 13 | + | |
| 14 | +define ("TABOOLA_OPTION_NAME","taboola_plugin_version"); // Note: 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 | 22 | |
| @@ -15,9 +21,11 @@ | ||
| 15 | 21 | define ("TABOOLA_CONTENT_FORMAT_HTML",'html'); |
| 16 | 22 | |
| 17 | 23 | |
| 18 | 24 | include_once('widget.php'); |
| 25 | +require_once('JavaScriptWrapper.php'); | |
| 19 | 26 | |
| 27 | + | |
| 20 | 28 | if (!class_exists('TaboolaWP')) { |
| 21 | 29 | class TaboolaWP |
| 22 | 30 | { |
| 23 | 31 | //save internal data |
| @@ -23,10 +31,28 @@ | ||
| 23 | 31 | //save internal data |
| 24 | 32 | public $data = array(); |
| 25 | 33 | public $_is_widget_on_page; |
| 26 | 34 | public $_is_head_script_loaded = false; |
| 35 | + private $tpl_sw = 'importScripts("https://cdn.taboola.com/webpush/tsw.js");'; | |
| 27 | 36 | |
| 28 | - function TaboolaWP() | |
| 37 | + private $msg_sw_error = <<<SWE | |
| 38 | + <p> | |
| 39 | + The file sw.js in the root directory of Wordpress is not writable. | |
| 40 | + Please change its permissions and try again. Otherwise replace its contents manually: | |
| 41 | + </p> | |
| 42 | + <pre><code>{{SW}}</code></pre> | |
| 43 | + <p> | |
| 44 | + Also make sure that the file is accessible at {{DOMAIN}}/sw.js | |
| 45 | + </p> | |
| 46 | + SWE; | |
| 47 | + | |
| 48 | + public $plugin_name; | |
| 49 | + public $plugin_directory; | |
| 50 | + public $plugin_url; | |
| 51 | + public $settings; | |
| 52 | + public $tbl_taboola_settings; | |
| 53 | + | |
| 54 | + public function __construct() | |
| 29 | 55 | { |
| 30 | 56 | global $wpdb; |
| 31 | 57 | |
| 32 | 58 | //initialize plugin constant |
| @@ -42,34 +68,94 @@ | ||
| 42 | 68 | |
| 43 | 69 | $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings'; |
| 44 | 70 | |
| 45 | 71 | //activation function |
| 46 | - register_activation_hook($this->plugin_name, array(&$this, 'activate')); | |
| 72 | + register_activation_hook($this->plugin_name, array(&$this, 'update_db')); | |
| 73 | + add_action('admin_init', array(&$this, 'update_db')); | |
| 47 | 74 | |
| 48 | 75 | // Enable sidebar widgets |
| 49 | - if($this->settings != NULL && !empty($this->settings->publisher_id)){ | |
| 76 | + if ($this->settings != NULL && !empty($this->settings->publisher_id)){ | |
| 50 | 77 | //register Taboola widget |
| 51 | 78 | add_action('widgets_init', |
| 52 | - create_function('', 'return register_widget("WP_Widget_Taboola");') | |
| 79 | + function(){ | |
| 80 | + return register_widget("WP_Widget_Taboola"); | |
| 81 | + } | |
| 53 | 82 | ); |
| 54 | 83 | } |
| 55 | 84 | |
| 85 | + //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled; | |
| 86 | + | |
| 56 | 87 | if (is_admin()) { |
| 57 | 88 | //add menu for plugin |
| 58 | 89 | add_action( 'admin_menu', array(&$this, 'admin_generate_menu') ); |
| 59 | 90 | add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 ); |
| 60 | - } else { | |
| 61 | - if($this->settings != NULL){ | |
| 91 | + }elseif ($this->settings != NULL){ | |
| 62 | 92 | add_action('wp_head', array(&$this, 'taboola_header_loader_inject')); |
| 93 | + if (!empty($this->settings->publisher_id_push)) { | |
| 94 | + add_action('wp_head', array(&$this, 'taboola_webpush_loader_js')); | |
| 95 | + | |
| 96 | + $sw = 'sw.js'; | |
| 97 | + $sw_path = ABSPATH . $sw; | |
| 98 | + | |
| 99 | + $content = file_exists($sw_path) ? file_get_contents($sw_path) : ''; | |
| 100 | + | |
| 101 | + if (strpos($content, $this->tpl_sw) === false) { | |
| 102 | + if (!is_writable(ABSPATH) || (file_exists($sw_path) && !is_writable($sw_path))) { | |
| 103 | + return $this->notice($this->msg_sw_error); | |
| 104 | + } | |
| 105 | + $content = $this->tpl_sw . PHP_EOL . $content; | |
| 106 | + if (file_put_contents($sw_path, $content) === false) { | |
| 107 | + return $this->notice($this->msg_sw_error); | |
| 108 | + } | |
| 109 | + } | |
| 110 | + } | |
| 63 | 111 | add_action('wp_footer', array(&$this, 'taboola_footer_loader_js')); |
| 64 | 112 | add_filter('the_content', array(&$this, 'load_taboola_content')); |
| 65 | - } | |
| 66 | - } | |
| 67 | - } | |
| 113 | + add_filter('the_content', array(&$this, 'load_taboola_content_mid')); | |
| 114 | + //add_filter('the_content', array(&$this, 'load_taboola_content_home')); | |
| 115 | + // add_filter('the_excerpt', array(&$this, 'load_taboola_content_home')); | |
| 116 | + // Homepage widget – capture the full page, inject once. | |
| 117 | + // Homepage widget – start buffering *before* anything else renders | |
| 118 | + add_action( 'template_redirect', [ $this, 'tb_home_buffer_start' ], 0 ); | |
| 68 | 119 | |
| 120 | + // Homepage widget – flush the buffer *after* literally everything else | |
| 121 | + add_action( 'shutdown', [ $this, 'tb_home_buffer_flush' ], PHP_INT_MAX ); | |
| 69 | 122 | |
| 123 | + | |
| 124 | + } | |
| 125 | + | |
| 126 | + } | |
| 127 | + | |
| 128 | + /* ------------------------------------------------------------------ | |
| 129 | + * HOMEPAGE WIDGET – full-page buffer | |
| 130 | + * ----------------------------------------------------------------*/ | |
| 131 | +public function tb_home_buffer_start() { | |
| 132 | + | |
| 133 | + // Use the plug-in’s own logic to decide if the home widget is enabled | |
| 134 | + if ( $this->should_show_content_widget_home() ) { | |
| 135 | + ob_start( [ $this, 'tb_home_buffer_inject' ] ); | |
| 136 | + } | |
| 137 | +} | |
| 70 | 138 | |
| 139 | +public function tb_home_buffer_inject( $html ) { | |
| 140 | + //error_log( '🟢 Taboola buffer inject ran. HTML length = ' . strlen( $html ) ); // LOG #1 | |
| 141 | + // error_log( 'TB-DEBUG â‘ buffer inject, html len='.strlen($html) ); | |
| 71 | 142 | |
| 143 | + /* | |
| 144 | + * load_taboola_content_home() already: | |
| 145 | + * – reads your saved widget-ID, placement, selector & occurrence | |
| 146 | + * – builds the HTML + JS | |
| 147 | + * – calls embed_taboola_content_location_home() to place it | |
| 148 | + */ | |
| 149 | + return $this->load_taboola_content_home( $html ); | |
| 150 | +} | |
| 151 | + | |
| 152 | +public function tb_home_buffer_flush() { | |
| 153 | + if ( ob_get_length() ) { | |
| 154 | + echo ob_get_clean(); | |
| 155 | + } | |
| 156 | +} | |
| 157 | + | |
| 72 | 158 | function plugin_action_links($links, $file) { |
| 73 | 159 | static $this_plugin; |
| 74 | 160 | |
| 75 | 161 | if (!$this_plugin) { |
| @@ -88,8 +174,27 @@ | ||
| 88 | 174 | $retVal = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->first_bc_enabled && trim($this->settings->first_bc_widget_id) != ''); |
| 89 | 175 | return $retVal; |
| 90 | 176 | } |
| 91 | 177 | |
| 178 | + private function should_show_content_widget_mid(){ | |
| 179 | + // PC - only if v2 was installed: | |
| 180 | + if (!$this->is_db_updated_for_min_ver("2.0.0")) | |
| 181 | + return false; | |
| 182 | + $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->mid_enabled && trim($this->settings->mid_widget_id) != ''); | |
| 183 | + return $retVal1; | |
| 184 | + } | |
| 185 | + | |
| 186 | + private function should_show_content_widget_home(){ | |
| 187 | + // PC - only if v2 was installed: | |
| 188 | + | |
| 189 | + //error_log( print_r( $this->settings, true ) ); // TEMP line | |
| 190 | + | |
| 191 | + if (!$this->is_db_updated_for_min_ver("2.0.0")) | |
| 192 | + return false; | |
| 193 | + $retVal2 = ((trim($this->settings->publisher_id) != '') && (is_front_page() || is_home()) && $this->settings->home_enabled && trim($this->settings->home_widget_id) != ''); | |
| 194 | + return $retVal2; | |
| 195 | + } | |
| 196 | + | |
| 92 | 197 | private function should_show_sidebar_widget(){ |
| 93 | 198 | $retVal = ((trim($this->settings->publisher_id) != '') && is_active_widget( false, false, TABOOLA_WIDGET_BASE_ID, true )); |
| 94 | 199 | return $retVal; |
| 95 | 200 | } |
| @@ -95,16 +200,15 @@ | ||
| 95 | 200 | } |
| 96 | 201 | |
| 97 | 202 | // Determine if a taboola widget should be added somewhere on the current page (content or sidebar) |
| 98 | 203 | function is_widget_on_page(){ |
| 99 | - return $this->should_show_content_widget() || $this->should_show_sidebar_widget(); | |
| 204 | + return $this->should_show_content_widget() || $this->should_show_content_widget_mid() || $this->should_show_content_widget_home() || $this->should_show_sidebar_widget(); | |
| 100 | 205 | } |
| 101 | 206 | |
| 102 | 207 | function get_page_type(){ |
| 103 | - | |
| 104 | 208 | $page_type='article'; |
| 105 | 209 | if (is_front_page()){ |
| 106 | - $page_type='home'; | |
| 210 | + $page_type='home'; | |
| 107 | 211 | }else if (is_category() || is_archive() || is_search()){ |
| 108 | 212 | $page_type='category'; |
| 109 | 213 | } |
| 110 | 214 | return $page_type; |
| @@ -113,17 +217,25 @@ | ||
| 113 | 217 | // return the head loader script |
| 114 | 218 | function taboola_header_loader_js() { |
| 115 | 219 | $head_string = ""; |
| 116 | 220 | |
| 117 | - // Only adding Script1 if a widget is going to be placed on the page. | |
| 221 | + // Only adding the loader if a widget is going to be placed on the page. | |
| 118 | 222 | if ($this->is_widget_on_page()){ |
| 223 | + // PC - since these params will be inserted in 'loaderInjectionScript.js' via search and replace, | |
| 224 | + // double brackets are used to ensure that each key is a unique string. | |
| 225 | + $stringParams = array( | |
| 226 | + '{{PUBLISHER_ID}}' => $this->settings->publisher_id, | |
| 227 | + '{{PAGE_TYPE}}' => $this->get_page_type(), | |
| 228 | + '{{WORDPRESS_VERSION}}' => get_bloginfo('version'), | |
| 229 | + '{{PHP_VERSION}}' => phpversion(), | |
| 230 | + '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION, | |
| 231 | + '{{LOC_MID}}' => $this->settings->mid_location_string, | |
| 232 | + '{{LOC_HOME}}' => $this->settings->home_location_string | |
| 233 | + ); | |
| 119 | 234 | |
| 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>'; | |
| 235 | + $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams); | |
| 236 | + $head_string = $scriptWrapper->getScriptMarkupString(); | |
| 124 | 237 | } |
| 125 | - | |
| 126 | 238 | return $head_string; |
| 127 | 239 | } |
| 128 | 240 | |
| 129 | 241 | |
| @@ -128,51 +240,97 @@ | ||
| 128 | 240 | |
| 129 | 241 | |
| 130 | 242 | // This function is used for the hook action, injects the header content to the <head> tag. |
| 131 | 243 | function taboola_header_loader_inject(){ |
| 132 | - | |
| 133 | 244 | echo $this->taboola_header_loader_js(); |
| 134 | 245 | } |
| 135 | 246 | |
| 247 | + // adding webpush loader | |
| 248 | + function taboola_webpush_loader_js() { | |
| 249 | + if(is_single() || is_home() || is_category() || is_front_page()){ | |
| 250 | + $stringParamsWeb = array( | |
| 251 | + '{{PUBLISHER_ID}}' => $this->settings->publisher_id_push | |
| 252 | + ); | |
| 253 | + $webpushInjectionScript = new JavaScriptWrapper("webPush.js",$stringParamsWeb); | |
| 254 | + echo $webpushInjectionScript; | |
| 255 | + } | |
| 256 | + } | |
| 136 | 257 | |
| 137 | 258 | function taboola_footer_loader_js() { |
| 138 | 259 | |
| 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>'; | |
| 260 | + // Only adding flush script if a widget is going to be placed on the page. | |
| 261 | + if ($this->is_widget_on_page()){ | |
| 262 | + $flushInjectionScript = new JavaScriptWrapper('flushInjectionScript.js',array()); | |
| 263 | + echo $flushInjectionScript->getScriptMarkupString(); | |
| 143 | 264 | } |
| 144 | 265 | } |
| 145 | 266 | |
| 267 | + // Below-article widget | |
| 146 | 268 | function load_taboola_content($content) |
| 147 | 269 | { |
| 148 | 270 | $taboola_content = array(); |
| 149 | 271 | if ($this->should_show_content_widget()){ |
| 150 | 272 | |
| 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); | |
| 273 | + $firstWidgetParams = array( | |
| 274 | + '{{WIDGET_ID}}' => $this->settings->first_bc_widget_id, | |
| 275 | + '{{CONTAINER}}' => 'taboola-below-article-thumbnails', | |
| 276 | + // PC - If v2 was not installed, then use v1 logic | |
| 277 | + '{{PLACEMENT}}' => (!empty($this->settings->first_bc_placement)) ? $this->settings->first_bc_placement : 'below-article' // In case v1 upgrade has not run yet | |
| 278 | + ); | |
| 154 | 279 | |
| 155 | - $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = '<div id="taboola-below-article"></div>'; | |
| 156 | - $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $script2_content; | |
| 280 | + $firstWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$firstWidgetParams); | |
| 281 | + $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-below-article-thumbnails'></div>"; | |
| 282 | + $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $firstWidgetScript; | |
| 157 | 283 | |
| 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); | |
| 284 | + $content = $this->embed_taboola_content_location($content,$taboola_content); | |
| 285 | + } | |
| 163 | 286 | |
| 164 | - $taboola_content[TABOOLA_CONTENT_FORMAT_HTML][] = '<div id="taboola-below-article-second"></div>'; | |
| 165 | - $taboola_content[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $script2_content; | |
| 166 | - } | |
| 287 | + return $content; | |
| 288 | + } | |
| 167 | 289 | |
| 168 | - $content = $this->embed_taboola_content_location($content,$taboola_content,trim($this->settings->location_string)); | |
| 290 | + // Mid-article-widget | |
| 291 | + function load_taboola_content_mid($content) | |
| 292 | + { | |
| 293 | + $taboola_content_mid = array(); | |
| 294 | + if ($this->should_show_content_widget_mid()){ | |
| 295 | + | |
| 296 | + $secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->mid_widget_id, | |
| 297 | + '{{CONTAINER}}' => 'taboola-mid-article-thumbnails', | |
| 298 | + '{{PLACEMENT}}' => $this->settings->mid_placement); | |
| 299 | + | |
| 300 | + $secondWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$secondWidgetParams); | |
| 301 | + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-mid-article-thumbnails'></div>"; | |
| 302 | + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $secondWidgetScript; | |
| 303 | + | |
| 304 | + $content = $this->embed_taboola_content_location_mid($content,$taboola_content_mid,trim($this->settings->mid_location_string)); | |
| 169 | 305 | } |
| 170 | 306 | |
| 171 | 307 | return $content; |
| 172 | 308 | } |
| 173 | 309 | |
| 310 | + // Homepage widget | |
| 311 | + function load_taboola_content_home($content) | |
| 312 | + { | |
| 313 | + //error_log( 'TB-DEBUG â‘¡ entered load_taboola_content_home' ); | |
| 314 | + $taboola_content_home = array(); | |
| 315 | + if ($this->should_show_content_widget_home()){ | |
| 174 | 316 | |
| 317 | + $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id, | |
| 318 | + '{{CONTAINER}}' => 'taboola-homepage-thumbnails', | |
| 319 | + '{{PLACEMENT}}' => $this->settings->home_placement); | |
| 320 | + | |
| 321 | + $homeWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$homeWidgetParams); | |
| 322 | + $taboola_content_home[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-homepage-thumbnails'></div>"; | |
| 323 | + $taboola_content_home[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $homeWidgetScript; | |
| 324 | + | |
| 325 | + $content = $this->embed_taboola_content_location_home($content,$taboola_content_home,trim($this->settings->home_location_string)); | |
| 326 | + } | |
| 327 | + return $content; | |
| 328 | + | |
| 329 | + } | |
| 330 | + | |
| 331 | + // Below-article widget | |
| 332 | + | |
| 175 | 333 | // Extract the taboola content in the required format: |
| 176 | 334 | // String - for injecting on the servr side |
| 177 | 335 | // Script or HTML - for injecting on the client side |
| 178 | 336 | function format_taboola_content($taboola_content,$format){ |
| @@ -194,28 +352,98 @@ | ||
| 194 | 352 | return $ret_val; |
| 195 | 353 | } |
| 196 | 354 | |
| 197 | 355 | |
| 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){ | |
| 356 | + // Mid-article widget | |
| 357 | + function format_taboola_content_mid($taboola_content_mid,$format){ | |
| 358 | + $ret_val = null; | |
| 359 | + | |
| 360 | + switch($format){ | |
| 361 | + case TABOOLA_CONTENT_FORMAT_STRING: | |
| 362 | + $result_string = join("",$taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML]). | |
| 363 | + "<script type='text/javascript'>".join("\n",$taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>"; | |
| 364 | + $ret_val = $result_string; | |
| 365 | + break; | |
| 366 | + | |
| 367 | + // script or html | |
| 368 | + default: | |
| 369 | + $ret_val = str_replace("\n","",join("",$taboola_content_mid[$format])); | |
| 370 | + break; | |
| 371 | + } | |
| 372 | + | |
| 373 | + return $ret_val; | |
| 374 | + } | |
| 375 | + | |
| 376 | + | |
| 377 | + // Homepage widget | |
| 378 | + function format_taboola_content_home($taboola_content_home,$format){ | |
| 379 | + | |
| 380 | + $ret_val = null; | |
| 381 | + | |
| 382 | + switch($format){ | |
| 383 | + case TABOOLA_CONTENT_FORMAT_STRING: | |
| 384 | + $result_string = join("",$taboola_content_home[TABOOLA_CONTENT_FORMAT_HTML]). | |
| 385 | + "<script type='text/javascript'>".join("\n",$taboola_content_home[TABOOLA_CONTENT_FORMAT_SCRIPT])."</script>"; | |
| 386 | + $ret_val = $result_string; | |
| 387 | + break; | |
| 388 | + | |
| 389 | + // script or html | |
| 390 | + default: | |
| 391 | + $ret_val = str_replace("\n","",join("",$taboola_content_home[$format])); | |
| 392 | + break; | |
| 393 | + } | |
| 394 | + | |
| 395 | + return $ret_val; | |
| 396 | + | |
| 397 | + } | |
| 398 | + | |
| 399 | + // Below-article widget | |
| 400 | + // Do the actual logic of choosing where to place the taboola content. | |
| 401 | + function embed_taboola_content_location($content, $taboola_content){ | |
| 200 | 402 | $do_default = true; |
| 201 | 403 | |
| 404 | + // tag is placed outside of content in order to allow "read more" functionality. | |
| 405 | + if ($this->settings->out_of_content_enabled){ | |
| 406 | + | |
| 407 | + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( | |
| 408 | + "{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML), | |
| 409 | + "{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT)) | |
| 410 | + ); | |
| 411 | + $scriptWrapper->appendScript("injectWidgetByMarker('tbmarker');"); | |
| 412 | + $content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$scriptWrapper."</script>"; | |
| 413 | + $do_default = false; | |
| 414 | + } | |
| 415 | + | |
| 416 | + // Default for below-article widget - add to the end of the content | |
| 417 | + if ($do_default){ | |
| 418 | + $content = $content.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING); | |
| 419 | + } | |
| 420 | + | |
| 421 | + return $content; | |
| 422 | + } | |
| 423 | + | |
| 424 | + // Mid-article widget | |
| 425 | + // Do the actual logic of choosing where to place the taboola content based on the "location" attribute | |
| 426 | + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location){ | |
| 427 | + $do_default = true; | |
| 428 | + | |
| 202 | 429 | if (isset($location) && $location != ''){ |
| 203 | 430 | $first_char = substr($location,0,1); |
| 204 | 431 | |
| 205 | 432 | // DIV/XPATH provided for JS handling |
| 206 | - if ($first_char == JS_MARKER){ | |
| 207 | - $full_indicator = substr($location,0,strlen(JS_INDICATOR)); | |
| 433 | + if ($first_char == TABOOLA_JS_MARKER){ | |
| 434 | + $full_indicator = substr($location,0,strlen(TABOOLA_JS_INDICATOR)); | |
| 208 | 435 | |
| 209 | - if ($full_indicator == JS_INDICATOR){ | |
| 436 | + if ($full_indicator == TABOOLA_JS_INDICATOR){ | |
| 210 | 437 | |
| 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); | |
| 438 | + $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR)); | |
| 439 | + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( | |
| 440 | + "{{HTML}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_HTML), | |
| 441 | + "{{SCRIPT}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_SCRIPT)) | |
| 442 | + ); | |
| 443 | + $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');"); | |
| 444 | + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>"; | |
| 216 | 445 | |
| 217 | - $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$js_inject_script."</script>"; | |
| 218 | 446 | $do_default = false; |
| 219 | 447 | } |
| 220 | 448 | |
| 221 | 449 | // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm) |
| @@ -220,37 +448,101 @@ | ||
| 220 | 448 | |
| 221 | 449 | // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm) |
| 222 | 450 | // basically it's CSS selectors like in jQuery |
| 223 | 451 | } else{ |
| 224 | - | |
| 225 | 452 | require_once('simple_html_dom.php'); |
| 226 | 453 | |
| 227 | 454 | $html_doc = str_get_html($content); |
| 228 | - $target_location = $html_doc->find($location,0); | |
| 455 | + $target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1); | |
| 229 | 456 | |
| 230 | 457 | // if the location was found within the html content |
| 231 | 458 | if (isset($target_location) && is_object($target_location)){ |
| 232 | 459 | |
| 233 | 460 | // adding taboola content AFTER the target location |
| 234 | - $target_location->outertext = $target_location->outertext.$this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_STRING); | |
| 461 | + $target_location->outertext = $target_location->outertext.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING); | |
| 235 | 462 | $content = $html_doc; |
| 236 | 463 | $do_default = false; |
| 237 | 464 | } |
| 238 | 465 | } |
| 239 | 466 | } |
| 467 | + // Default for below-article widget - add to the end of the content | |
| 468 | + // if ($do_default){ | |
| 469 | + // $content = $content.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING); | |
| 470 | + // } | |
| 240 | 471 | |
| 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 | 472 | return $content; |
| 247 | 473 | } |
| 248 | 474 | |
| 475 | + // Homepage widget | |
| 476 | + // Do the actual logic of choosing where to place the taboola content based on the "location" attribute | |
| 477 | + function embed_taboola_content_location_home($content, $taboola_content_home, $location){ | |
| 249 | 478 | |
| 479 | + | |
| 480 | + // error_log( 'TB-DEBUG â‘¢ raw selector string: [' . $location . ']' ); | |
| 481 | + | |
| 482 | + // load parser | |
| 483 | + //require_once plugin_dir_path( __FILE__ ) . 'simple_html_dom.php'; | |
| 484 | + // $html_doc = str_get_html( $content ); | |
| 485 | + | |
| 486 | + // occurrence: settings is 1-based, find() wants 0-based | |
| 487 | + // $occurrence = max( 1, intval( $this->settings->home_location_string_occurrence ) ); | |
| 488 | + // $node = $html_doc->find( $location, $occurrence - 1 ); | |
| 489 | + // $found = is_object( $node ) ? 1 : 0; | |
| 490 | + | |
| 491 | + // error_log( "TB-DEBUG â‘¢ found {$found} matching node(s) for selector [{$location}]" ); | |
| 492 | + // ──────────────────────────── | |
| 493 | + $do_default = true; | |
| 494 | + | |
| 495 | + if (isset($location) && $location != ''){ | |
| 496 | + $first_char = substr($location,0,1); | |
| 497 | + | |
| 498 | + // DIV/XPATH provided for JS handling | |
| 499 | + if ($first_char == TABOOLA_JS_MARKER){ | |
| 500 | + $full_indicator = substr($location,0,strlen(TABOOLA_JS_INDICATOR)); | |
| 501 | + | |
| 502 | + if ($full_indicator == TABOOLA_JS_INDICATOR){ | |
| 503 | + | |
| 504 | + $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR)); | |
| 505 | + $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( | |
| 506 | + "{{HTML}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_HTML), | |
| 507 | + "{{SCRIPT}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_SCRIPT)) | |
| 508 | + ); | |
| 509 | + $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');"); | |
| 510 | + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>"; | |
| 511 | + | |
| 512 | + $do_default = false; | |
| 513 | + } | |
| 514 | + | |
| 515 | + // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm) | |
| 516 | + // basically it's CSS selectors like in jQuery | |
| 517 | + } else{ | |
| 518 | + require_once('simple_html_dom.php'); | |
| 519 | + | |
| 520 | + $html_doc = str_get_html($content); | |
| 521 | + $target_location = $html_doc->find($location,($this->settings->home_location_string_occurrence)-1); | |
| 522 | + | |
| 523 | + // if the location was found within the html content | |
| 524 | + if (isset($target_location) && is_object($target_location)){ | |
| 525 | + | |
| 526 | + // adding taboola content AFTER the target location | |
| 527 | + $target_location->outertext = $target_location->outertext.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING); | |
| 528 | + $content = $html_doc; | |
| 529 | + $do_default = false; | |
| 530 | + } | |
| 531 | + } | |
| 532 | + } | |
| 533 | + // Default for below-article widget - add to the end of the content | |
| 534 | + if ($do_default){ | |
| 535 | + $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING); | |
| 536 | + } | |
| 537 | + return $content; | |
| 538 | + | |
| 539 | + | |
| 540 | + } | |
| 541 | + | |
| 250 | 542 | function admin_generate_menu(){ |
| 251 | 543 | global $current_user; |
| 252 | - add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110); | |
| 544 | + add_menu_page(__('Taboola','taboola_widget'), __('Taboola','taboola_widget'), 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110); | |
| 253 | 545 | } |
| 254 | 546 | |
| 255 | 547 | function admin_taboola_settings(){ |
| 256 | 548 | global $wpdb; |
| @@ -257,57 +549,283 @@ | ||
| 257 | 549 | $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 258 | 550 | $taboola_errors = array(); |
| 259 | 551 | if($_SERVER['REQUEST_METHOD'] == 'POST'){ |
| 260 | 552 | |
| 261 | - if(trim($_POST['publisher_id']) == ''){ | |
| 262 | - $taboola_errors[] = "Please add a 'Publisher ID' in order to apply changes to your widgets"; | |
| 553 | + if(trim(strip_tags($_POST['publisher_id'])) == ''){ | |
| 554 | + $taboola_errors[] = "Publisher ID"; | |
| 263 | 555 | } |
| 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']) == '') | |
| 266 | - ){ | |
| 267 | - $taboola_errors[] = "Please add a 'Widget ID' in order to apply changes to your widgets"; | |
| 556 | + | |
| 557 | + if(isset($_POST['web_push_enabled'])) { | |
| 558 | + if (trim(strip_tags($_POST['publisher_id_push'])) == '') { | |
| 559 | + $taboola_errors[] = "Publisher Push ID"; | |
| 560 | + } | |
| 268 | 561 | } |
| 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"; | |
| 562 | + | |
| 563 | + if(isset($_POST['first_bc_enabled'])) { | |
| 564 | + if (trim(strip_tags($_POST['first_bc_widget_id'])) == '') { | |
| 565 | + $taboola_errors[] = "Below-article > Widget ID"; | |
| 566 | + } | |
| 567 | + if (trim(strip_tags($_POST['first_bc_placement'])) == '') { | |
| 568 | + $taboola_errors[] = "Below-article > Placement Name"; | |
| 569 | + } | |
| 271 | 570 | } |
| 571 | + | |
| 572 | + if(isset($_POST['mid_enabled'])) { | |
| 573 | + if (trim(strip_tags($_POST['mid_widget_id'])) == '') { | |
| 574 | + $taboola_errors[] = "Mid-article > Widget ID"; | |
| 575 | + } | |
| 576 | + if (trim(strip_tags($_POST['mid_placement'])) == '') { | |
| 577 | + $taboola_errors[] = "Mid-article > Placement Name"; | |
| 578 | + } | |
| 579 | + if (trim(strip_tags($_POST['mid_location_string'])) == '') { | |
| 580 | + $taboola_errors[] = "Mid-article > CSS selector"; | |
| 581 | + } | |
| 582 | + else { | |
| 583 | + // If the 'location' WAS filled in, then... | |
| 584 | + | |
| 585 | + // 1) Check for a valid 'location': | |
| 586 | + | |
| 587 | + // Validation method has not been implemented | |
| 588 | + // mid_if (!$this->is_locaton_string_valid1($_POST['mid_location_string'])) { | |
| 589 | + // $taboola_errors[] = "Mid-article > CSS Selector (invalid value)"; | |
| 590 | + // } | |
| 591 | + | |
| 592 | + // 2) Validate the 'occurrence': | |
| 593 | + if (!$this->is_mid_location_string_occurrence_valid($_POST['mid_location_string_occurrence'])) { | |
| 594 | + $taboola_errors[] = "Mid-article > Occurance (must be >= 1)"; | |
| 595 | + } | |
| 596 | + } | |
| 597 | + | |
| 598 | + } | |
| 599 | + | |
| 600 | + if(isset($_POST['home_enabled'])) { | |
| 601 | + if (trim(strip_tags($_POST['home_widget_id'])) == '') { | |
| 602 | + $taboola_errors[] = "Homepage > Widget ID"; | |
| 603 | + } | |
| 604 | + if (trim(strip_tags($_POST['home_placement'])) == '') { | |
| 605 | + $taboola_errors[] = "Homepage > Placement Name"; | |
| 606 | + } | |
| 607 | + | |
| 608 | + if (trim(strip_tags($_POST['home_location_string'])) == '') { | |
| 609 | + $taboola_errors[] = "Homepage > CSS selector"; | |
| 610 | + } | |
| 611 | + else { | |
| 612 | + // If the 'location' WAS filled in, then... | |
| 613 | + | |
| 614 | + // 1) Check for a valid 'location': | |
| 615 | + | |
| 616 | + // Validation method has not been implemented | |
| 617 | + // if (!$this->is_home_location_string_valid($_POST['home_location_string'])) { | |
| 618 | + // $taboola_errors[] = "Homepage > CSS Selector (invalid value)"; | |
| 619 | + // } | |
| 620 | + | |
| 621 | + // 2) Validate the 'occurrence': | |
| 622 | + if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) { | |
| 623 | + $taboola_errors[] = "Homepage > Occurance (must be >= 1)"; | |
| 624 | + } | |
| 625 | + } | |
| 626 | + } | |
| 627 | + | |
| 272 | 628 | if(count($taboola_errors) == 0){ |
| 273 | - | |
| 274 | 629 | $data = array( |
| 275 | 630 | "publisher_id" => trim($_POST['publisher_id']), |
| 276 | - "first_bc_enabled" => $_POST['first_bc_enabled'] == 'on' ? true : false, | |
| 277 | - "first_bc_widget_id" => trim($_POST['first_bc_widget_id']), | |
| 278 | - "second_bc_enabled" => $_POST['second_bc_enabled'] == 'on' ? true : false, | |
| 279 | - "second_bc_widget_id" => trim($_POST['second_bc_widget_id']), | |
| 280 | - "location_string" => trim($_POST['location_string']) | |
| 631 | + | |
| 632 | + "web_push_enabled" => isset($_POST['web_push_enabled']) ? true : false, | |
| 633 | + "publisher_id_push" => !empty($_POST['publisher_id_push']) ? trim($_POST['publisher_id_push']) : '', | |
| 634 | + | |
| 635 | + "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false, | |
| 636 | + "first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '', | |
| 637 | + "first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '', | |
| 638 | + | |
| 639 | + "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false, | |
| 640 | + | |
| 641 | + "mid_enabled" => isset($_POST['mid_enabled']) ? true : false, | |
| 642 | + "mid_widget_id" => !empty($_POST['mid_widget_id']) ? trim($_POST['mid_widget_id']) : '', | |
| 643 | + "mid_placement" => !empty($_POST['mid_placement']) ? trim($_POST['mid_placement']) : '', | |
| 644 | + "mid_paragraph_ui_mode" => !empty($_POST['mid_paragraph_ui_mode']) ? trim($_POST['mid_paragraph_ui_mode']) : '', | |
| 645 | + | |
| 646 | + "mid_location_string_occurrence" => !empty($_POST['mid_location_string_occurrence']) ? $_POST['mid_location_string_occurrence'] : '', | |
| 647 | + "mid_location_string" => !empty($_POST['mid_location_string']) ? trim($_POST['mid_location_string']) : '', | |
| 648 | + | |
| 649 | + "home_enabled" => isset($_POST['home_enabled']) ? true : false, | |
| 650 | + "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '', | |
| 651 | + "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '', | |
| 652 | + | |
| 653 | + "home_location_string_occurrence" => !empty($_POST['home_location_string_occurrence']) ? $_POST['home_location_string_occurrence'] : '', | |
| 654 | + "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : '' | |
| 281 | 655 | ); |
| 282 | 656 | |
| 283 | 657 | //var_dump($settings); |
| 284 | - if($settings == NULL){ | |
| 285 | - $wpdb->insert($this->tbl_taboola_settings, $data, null, null); | |
| 286 | - } else { | |
| 287 | - $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id)); | |
| 658 | + | |
| 659 | + $is_valid_nonce = false; | |
| 660 | + | |
| 661 | + if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) { | |
| 662 | + $is_valid_nonce = true; | |
| 288 | 663 | } |
| 664 | + | |
| 665 | + if ($is_valid_nonce) { | |
| 666 | + if($settings == NULL){ | |
| 667 | + $wpdb->insert($this->tbl_taboola_settings, $data, null, null); | |
| 668 | + } else { | |
| 669 | + $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id)); | |
| 670 | + } | |
| 671 | + } | |
| 289 | 672 | } |
| 290 | 673 | $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 291 | 674 | } |
| 675 | + include_once('settings.php'); | |
| 676 | + } | |
| 677 | + | |
| 678 | + function is_mid_location_string_valid($mid_location_string){ | |
| 679 | + // TODO:: validate the location string | |
| 680 | + return true; | |
| 681 | + } | |
| 292 | 682 | |
| 683 | + function is_mid_location_string_occurrence_valid($mid_location_string_occurrence){ | |
| 293 | 684 | |
| 294 | - include_once('settings.php'); | |
| 685 | + if ((int)$mid_location_string_occurrence >= 1) { | |
| 686 | + return true; | |
| 687 | + } | |
| 688 | + return false; | |
| 295 | 689 | } |
| 296 | 690 | |
| 297 | - function is_location_string_valid($location_string){ | |
| 691 | + function is_home_location_string_valid($home_location_string){ | |
| 298 | 692 | // TODO:: validate the location string |
| 299 | 693 | return true; |
| 300 | 694 | } |
| 301 | - function activate(){ | |
| 695 | + | |
| 696 | + function is_home_location_string_occurrence_valid($home_location_string_occurrence){ | |
| 697 | + | |
| 698 | + if ((int)$home_location_string_occurrence >= 1) { | |
| 699 | + return true; | |
| 700 | + } | |
| 701 | + return false; | |
| 702 | + } | |
| 703 | + | |
| 704 | + function columnExists($column_name) { | |
| 302 | 705 | global $wpdb; |
| 706 | + $table_name = $wpdb->prefix . "_taboola_settings"; | |
| 707 | + tb_write_log("table name : " . $table_name); | |
| 708 | + tb_write_log("column name : " . $column_name); | |
| 303 | 709 | |
| 710 | + $column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE '$column_name'") !== null; | |
| 711 | + | |
| 712 | + if ($column_exists) { | |
| 713 | + tb_write_log("Yes - column " . $column_name . " DOES exist!"); //PC | |
| 714 | + return true; | |
| 715 | + } else { | |
| 716 | + tb_write_log("No - column " . $column_name . " does NOT exist!"); //PC | |
| 717 | + return false; | |
| 718 | + } | |
| 719 | + } | |
| 720 | + | |
| 721 | + function is_upgrade_from_v1() { | |
| 722 | + | |
| 723 | + if($this->columnExists("first_bc_widget_id") && !$this->columnExists("first_bc_placement")){ | |
| 724 | + tb_write_log("This IS a v1 upgrade!"); //PC | |
| 725 | + return true; | |
| 726 | + } | |
| 727 | + else { | |
| 728 | + tb_write_log("This is NOT a v1 upgrade!"); //PC | |
| 729 | + return false; | |
| 730 | + } | |
| 731 | + } | |
| 732 | + | |
| 733 | + function is_db_updated_for_min_ver($min_ver) { | |
| 734 | + /** | |
| 735 | + * Checks if the DB was *previously* updated for the minimum version specified. | |
| 736 | + * $min_ver should be passed in a format like this: "2.1.0" | |
| 737 | + */ | |
| 738 | + | |
| 739 | + global $wpdb; | |
| 740 | + | |
| 741 | + // PC - Temporary patch for v2.1.0 | |
| 742 | + | |
| 743 | + // START patch =============================================== | |
| 744 | + // Check if the `_taboola` table exists. | |
| 745 | + // If not, then return false. | |
| 746 | + | |
| 747 | + $table_name = $wpdb->prefix . "_taboola_settings"; | |
| 748 | + | |
| 749 | + if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { | |
| 750 | + tb_write_log("Table NOT FOUND"); | |
| 751 | + return false; | |
| 752 | + } | |
| 753 | + else { | |
| 754 | + tb_write_log("Table EXISTS"); | |
| 755 | + } | |
| 756 | + // END patch ================================================= | |
| 757 | + | |
| 758 | + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found. | |
| 759 | + $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0'); | |
| 760 | + | |
| 761 | + $db_is_up_to_date = version_compare($saved_version, $min_ver, '>='); | |
| 762 | + //tb_write_log($db_is_up_to_date); | |
| 763 | + return $db_is_up_to_date; | |
| 764 | + | |
| 765 | + } | |
| 766 | + | |
| 767 | + function is_db_updated_for_current_ver() { | |
| 768 | + /** | |
| 769 | + * DEPRECATED | |
| 770 | + * Checks if the DB was *previously* updated for the version currently loaded. | |
| 771 | + */ | |
| 772 | + global $wpdb; | |
| 773 | + | |
| 774 | + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found. | |
| 775 | + $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0'); | |
| 776 | + | |
| 777 | + // Get the currently loaded plugin version. | |
| 778 | + $plugin_version = $this->get_loaded_plugin_version(); | |
| 779 | + | |
| 780 | + $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>='); | |
| 781 | + //tb_write_log($db_is_up_to_date); | |
| 782 | + return $db_is_up_to_date; | |
| 783 | + | |
| 784 | + } | |
| 785 | + | |
| 786 | + function get_loaded_plugin_version() { | |
| 787 | + | |
| 788 | + $plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only | |
| 789 | + $loaded_plugin_version = $plugin_data['Version']; | |
| 790 | + return $loaded_plugin_version; | |
| 791 | + | |
| 792 | + } | |
| 793 | + | |
| 794 | + function save_taboola_version($min_ver) { | |
| 795 | + /** | |
| 796 | + * Checks for the saved version in wp_options. | |
| 797 | + * If not found - adds it. Else - updates it. | |
| 798 | + */ | |
| 799 | + | |
| 800 | + | |
| 801 | + tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC | |
| 802 | + | |
| 803 | + $saved_version = get_option(TABOOLA_OPTION_NAME, ''); | |
| 804 | + if ($saved_version == '') { | |
| 805 | + add_option(TABOOLA_OPTION_NAME, $min_ver); | |
| 806 | + } | |
| 807 | + else { | |
| 808 | + update_option(TABOOLA_OPTION_NAME, $min_ver); | |
| 809 | + } | |
| 810 | + } | |
| 811 | + | |
| 812 | + function update_db(){ | |
| 813 | + | |
| 814 | + // If we are up to date, then skip this method... | |
| 815 | + if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) { | |
| 816 | + //tb_write_log("All up to date!"); | |
| 817 | + return; | |
| 818 | + } | |
| 819 | + | |
| 820 | + $this->save_taboola_version(TABOOLA_MIN_VER); | |
| 821 | + | |
| 822 | + global $wpdb; | |
| 304 | 823 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 824 | + | |
| 305 | 825 | |
| 306 | - $settings_table = $this->tbl_taboola_settings; | |
| 307 | - | |
| 308 | - //check mysql version | |
| 309 | - if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) { | |
| 826 | + // Handling for older MySQL versions | |
| 827 | + if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) { | |
| 310 | 828 | if (!empty($wpdb->charset)) |
| 311 | 829 | $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; |
| 312 | 830 | if (!empty($wpdb->collate)) |
| 313 | 831 | $charset_collate .= " COLLATE $wpdb->collate"; |
| @@ -312,24 +830,47 @@ | ||
| 312 | 830 | if (!empty($wpdb->collate)) |
| 313 | 831 | $charset_collate .= " COLLATE $wpdb->collate"; |
| 314 | 832 | } |
| 315 | 833 | |
| 834 | + // Check if this is an upgrade from v1 | |
| 835 | + $is_upgrade_from_v1 = $this->is_upgrade_from_v1(); | |
| 836 | + | |
| 837 | + // tb_write_log("Is upgrade from v1: " . ($is_upgrade_from_v1 ? 'true' : 'false')); | |
| 838 | + | |
| 839 | + | |
| 316 | 840 | //settings table structure |
| 317 | 841 | $sql_table_settings = " |
| 318 | 842 | CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` ( |
| 319 | 843 | `id` INT NOT NULL AUTO_INCREMENT , |
| 320 | 844 | `publisher_id` VARCHAR(255) DEFAULT NULL, |
| 845 | + `web_push_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, | |
| 846 | + `publisher_id_push` INT(15) DEFAULT NULL, | |
| 321 | 847 | `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 322 | 848 | `first_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 849 | + `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .", | |
| 323 | 850 | `first_bc_custom_css` TEXT DEFAULT NULL, |
| 324 | 851 | `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 325 | 852 | `second_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 326 | 853 | `second_bc_custom_css` TEXT DEFAULT NULL, |
| 327 | 854 | `location_string` TEXT DEFAULT NULL, |
| 855 | + `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, | |
| 856 | + `mid_widget_id` VARCHAR(255) DEFAULT NULL, | |
| 857 | + `mid_placement` VARCHAR(255) DEFAULT NULL, | |
| 858 | + `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE, | |
| 859 | + `mid_location_string` TEXT DEFAULT NULL, | |
| 860 | + `mid_location_string_occurrence` SMALLINT DEFAULT NULL, | |
| 861 | + `mid_paragraph_ui_mode` VARCHAR(255) DEFAULT NULL, | |
| 862 | + `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, | |
| 863 | + `home_widget_id` VARCHAR(255) DEFAULT NULL, | |
| 864 | + `home_placement` VARCHAR(255) DEFAULT NULL, | |
| 865 | + `home_location_string` TEXT DEFAULT NULL, | |
| 866 | + `home_location_string_occurrence` SMALLINT DEFAULT NULL, | |
| 328 | 867 | PRIMARY KEY (`id`) |
| 329 | 868 | )" . $charset_collate . ";"; |
| 869 | + | |
| 870 | + // tb_write_log($sql_table_settings); | |
| 330 | 871 | |
| 331 | - // creat/update the table | |
| 872 | + // create/update the table | |
| 332 | 873 | dbDelta($sql_table_settings); |
| 333 | 874 | } |
| 334 | 875 | } |
| 335 | 876 | } |
| @@ -336,5 +877,65 @@ | ||
| 336 | 877 | |
| 337 | 878 | global $taboolaWP; |
| 338 | 879 | $taboolaWP = new TaboolaWP(); |
| 339 | 880 | |
| 881 | +/* | |
| 882 | +A few utility methods for logging | |
| 883 | +*/ | |
| 884 | + | |
| 885 | +// Writes debugging messages to 'logs/debug.log'. | |
| 886 | +// The exact file location depends on *where* you trigger the function. | |
| 887 | +// In this script, we trigger the function from the admin dashboard. | |
| 888 | +// So the log file is located under 'wp-admin/logs'. | |
| 889 | +function tb_write_log($log_msg) | |
| 890 | +{ | |
| 891 | + if (!TABOOLA_DEBUG_MODE) // If not debug mode, do nothing | |
| 892 | + return; | |
| 893 | + | |
| 894 | + $log_filename = "logs"; | |
| 895 | + if (!file_exists($log_filename)) | |
| 896 | + { | |
| 897 | + mkdir($log_filename, 0777, true); | |
| 898 | + } | |
| 899 | + $log_file_data = $log_filename.'/debug.log'; | |
| 900 | + | |
| 901 | + // Add date/time | |
| 902 | + $date = date('Y-m-d H:i:s'); | |
| 903 | + $log_msg_with_date = $date." : ".$log_msg; | |
| 904 | + | |
| 905 | + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND); | |
| 906 | +} | |
| 907 | + | |
| 908 | +// Write to console | |
| 909 | +function tb_console_log( $data ){ | |
| 910 | + | |
| 911 | + If(wp_get_environment_type() === 'development') { | |
| 912 | + | |
| 913 | + echo '<script>'; | |
| 914 | + echo 'console.log('. json_encode( $data ) .')'; | |
| 915 | + echo '</script>'; | |
| 916 | + | |
| 917 | + } | |
| 918 | +} | |
| 919 | + | |
| 920 | +// Write to webpage and stop | |
| 921 | +function tb_print_to_page($data) { | |
| 922 | + If(wp_get_environment_type() === 'development') { | |
| 923 | + print_r($data); | |
| 924 | + die; | |
| 925 | + } | |
| 926 | +} | |
| 340 | 927 | // |
| 928 | + | |
| 929 | +// Enqueue js_inject.min.js script for XPath injection | |
| 930 | +function enqueue_taboola_scripts() { | |
| 931 | + wp_register_script( | |
| 932 | + 'taboola-injector', | |
| 933 | + plugins_url('js/js_inject.min.js', __FILE__), | |
| 934 | + array(), // No dependencies | |
| 935 | + null, // No version specified | |
| 936 | + true // Load in footer | |
| 937 | + ); | |
| 938 | + | |
| 939 | + wp_enqueue_script('taboola-injector'); | |
| 940 | +} | |
| 941 | +add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts'); | |