| @@ -2,13 +2,13 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Plugin Name: Taboola |
| 4 | 4 | * Plugin URI: https://developers.taboola.com/web-integrations/docs/wordpress-plugin |
| 5 | 5 | * Description: Taboola |
| 6 | - * Version: 3.0.0 | |
| 6 | + * Version: 3.0.1 | |
| 7 | 7 | * Author: Taboola |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -define( 'TABOOLA_PLUGIN_VERSION', '3.0' ); // track every release | |
| 10 | +define( 'TABOOLA_PLUGIN_VERSION', '3.0.1' ); // track every release | |
| 11 | 11 | define( 'TABOOLA_MIN_VER', '3.0' ); // bump only when DB changes |
| 12 | 12 | define( 'TABOOLA_DEBUG_MODE', false ); |
| 13 | 13 | |
| 14 | 14 | define( 'TABOOLA_OPTION_NAME', 'taboola_plugin_version' ); |
| @@ -48,9 +48,9 @@ | ||
| 48 | 48 | |
| 49 | 49 | /* ─────────────────────────── constructor ─────────────────────────── */ |
| 50 | 50 | public function __construct() { |
| 51 | 51 | global $wpdb; |
| 52 | - | |
| 52 | + //initialize plugin constant | |
| 53 | 53 | define( 'TaboolaWP', true ); |
| 54 | 54 | |
| 55 | 55 | $this->_is_widget_on_page = false; |
| 56 | 56 | $this->_is_head_script_loaded = false; |
| @@ -81,11 +81,34 @@ | ||
| 81 | 81 | if ( is_admin() ) { |
| 82 | 82 | add_action( 'admin_menu', [ $this, 'admin_generate_menu' ] ); |
| 83 | 83 | add_filter( 'plugin_action_links', |
| 84 | 84 | [ $this, 'plugin_action_links' ], 10, 2 ); |
| 85 | - } elseif ( $this->settings ) { | |
| 85 | + } | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + elseif ( $this->settings ) { | |
| 86 | 91 | /* loader & flush */ |
| 87 | 92 | add_action( 'wp_head', [ $this, 'taboola_header_loader_inject' ] ); |
| 93 | + if ( ! empty( $this->settings->publisher_id_push ) ) { | |
| 94 | + add_action( 'wp_head', [ $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 | + } | |
| 88 | 111 | add_action( 'wp_footer', [ $this, 'taboola_footer_loader_js' ] ); |
| 89 | 112 | |
| 90 | 113 | /* content widgets */ |
| 91 | 114 | add_filter( 'the_content', [ $this, 'load_taboola_content' ] ); |
| @@ -245,15 +268,21 @@ | ||
| 245 | 268 | return $page_type; |
| 246 | 269 | } |
| 247 | 270 | |
| 248 | 271 | // return the head loader script |
| 249 | - function taboola_header_loader_js() { | |
| 250 | - $head_string = ""; | |
| 272 | + function taboola_header_loader_js() { | |
| 273 | + if ($this->is_widget_on_page()){ | |
| 274 | + | |
| 275 | + // New logic to get all mid-article locations | |
| 276 | + $mid_locations_string = ''; | |
| 277 | + if (!empty($this->settings->mid_widgets)) { | |
| 278 | + $mid_widgets_array = json_decode($this->settings->mid_widgets, true); | |
| 279 | + if (is_array($mid_widgets_array)) { | |
| 280 | + $locations = array_column($mid_widgets_array, 'location_string'); | |
| 281 | + $mid_locations_string = implode(', ', $locations); | |
| 282 | + } | |
| 283 | + } | |
| 251 | 284 | |
| 252 | - // Only adding the loader if a widget is going to be placed on the page. | |
| 253 | - if ($this->is_widget_on_page()){ | |
| 254 | - // PC - since these params will be inserted in 'loaderInjectionScript.js' via search and replace, | |
| 255 | - // double brackets are used to ensure that each key is a unique string. | |
| 256 | 285 | $stringParams = array( |
| 257 | 286 | '{{PUBLISHER_ID}}' => $this->settings->publisher_id, |
| 258 | 287 | '{{PAGE_TYPE}}' => $this->get_page_type(), |
| 259 | 288 | '{{WORDPRESS_VERSION}}' => get_bloginfo('version'), |
| @@ -258,18 +287,17 @@ | ||
| 258 | 287 | '{{PAGE_TYPE}}' => $this->get_page_type(), |
| 259 | 288 | '{{WORDPRESS_VERSION}}' => get_bloginfo('version'), |
| 260 | 289 | '{{PHP_VERSION}}' => phpversion(), |
| 261 | 290 | '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION, |
| 262 | - '{{LOC_MID}}' => $this->settings->mid_location_string, | |
| 263 | - '{{LOC_HOME}}' => $this->settings->home_location_string | |
| 291 | + '{{LOC_MID}}' => $mid_locations_string, | |
| 292 | + '{{LOC_HOME}}' => $this->settings->home_location_string ?? '' | |
| 264 | 293 | ); |
| 265 | 294 | |
| 266 | 295 | $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams); |
| 267 | - $head_string = $scriptWrapper->getScriptMarkupString(); | |
| 296 | + return $scriptWrapper->getScriptMarkupString(); | |
| 268 | 297 | } |
| 269 | - return $head_string; | |
| 298 | + return ""; | |
| 270 | 299 | } |
| 271 | - | |
| 272 | 300 | |
| 273 | 301 | // This function is used for the hook action, injects the header content to the <head> tag. |
| 274 | 302 | function taboola_header_loader_inject(){ |
| 275 | 303 | echo $this->taboola_header_loader_js(); |