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