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