| @@ -1,208 +1,155 @@ | ||
| 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.1.0 | |
| 7 | - * Author: Taboola | |
| 3 | + * Plugin Name: Taboola | |
| 4 | + * Plugin URI: https://developers.taboola.com/web-integrations/docs/wordpress-plugin | |
| 5 | + * Description: Taboola | |
| 6 | + * Version: 2.2.3 | |
| 7 | + * Author: Taboola | |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -define( 'TABOOLA_PLUGIN_VERSION', '3.1.0' ); // 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.2.3"); // => UPDATE THIS FOR *EVERY* RELEASE (USED FOR TRACKING) | |
| 11 | +define ("TABOOLA_MIN_VER","2.2.2"); // => UPDATE THIS *ONLY* IF THIS RELEASE HAS *DB CHANGES* | |
| 12 | +define ("TABOOLA_DEBUG_MODE", false); // => SET THIS TO 'FALSE' FOR *EVERY* RELEASE (USED TO SUPRESS DEBUGGING LOGS) | |
| 13 | 13 | |
| 14 | -define( 'TABOOLA_OPTION_NAME', 'taboola_plugin_version' ); | |
| 14 | +define ("TABOOLA_OPTION_NAME","taboola_plugin_version"); // Note: if this release has DB changes, then the min version will be saved under 'taboola_plugin_version' in 'wp_options'. | |
| 15 | 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> | |
| 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; | |
| 35 | + private $tpl_sw = 'importScripts("https://cdn.taboola.com/webpush/tsw.js");'; | |
| 36 | + | |
| 37 | + private $msg_sw_error = <<<SWE | |
| 38 | + <p> | |
| 39 | + The file sw.js in the root directory of Wordpress is not writable. | |
| 40 | + Please change its permissions and try again. Otherwise replace its contents manually: | |
| 41 | + </p> | |
| 41 | 42 | <pre><code>{{SW}}</code></pre> |
| 42 | - <p>Also make sure it's reachable at <code>{{DOMAIN}}/sw.js</code></p> | |
| 43 | -SWE; | |
| 43 | + <p> | |
| 44 | + Also make sure that the file is accessible at {{DOMAIN}}/sw.js | |
| 45 | + </p> | |
| 46 | + SWE; | |
| 44 | 47 | |
| 45 | - public $plugin_name; | |
| 46 | - public $plugin_directory; | |
| 47 | - public $plugin_url; | |
| 48 | - public $settings; | |
| 49 | - public $tbl_taboola_settings; | |
| 48 | + public $plugin_name; | |
| 49 | + public $plugin_directory; | |
| 50 | + public $plugin_url; | |
| 51 | + public $settings; | |
| 52 | + public $tbl_taboola_settings; | |
| 50 | 53 | |
| 51 | - /* ─────────────────────────── constructor ─────────────────────────── */ | |
| 52 | - public function __construct() { | |
| 53 | - global $wpdb; | |
| 54 | - //initialize plugin constant | |
| 55 | - define( 'TaboolaWP', true ); | |
| 54 | + public function __construct() | |
| 55 | + { | |
| 56 | + global $wpdb; | |
| 56 | 57 | |
| 57 | - $this->_is_widget_on_page = false; | |
| 58 | - $this->_is_head_script_loaded = false; | |
| 58 | + //initialize plugin constant | |
| 59 | + DEFINE('TaboolaWP', true); | |
| 59 | 60 | |
| 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 | - ); | |
| 61 | + $this->_is_widget_on_page = false; | |
| 62 | + $this->_is_head_script_loaded = false; | |
| 69 | 63 | |
| 70 | - /* activation / upgrade */ | |
| 71 | - register_activation_hook( $this->plugin_name, [ $this, 'update_db' ] ); | |
| 72 | - add_action( 'admin_init', [ $this, 'update_db' ] ); | |
| 64 | + $this->plugin_name = plugin_basename(__FILE__); | |
| 65 | + $this->plugin_directory = plugin_dir_path(__FILE__); | |
| 66 | + $this->plugin_url = trailingslashit(WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__))); | |
| 67 | + $this->settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); | |
| 73 | 68 | |
| 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 | - } | |
| 69 | + $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings'; | |
| 81 | 70 | |
| 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' ] ); | |
| 71 | + //activation function | |
| 72 | + register_activation_hook($this->plugin_name, array(&$this, 'update_db')); | |
| 73 | + add_action('admin_init', array(&$this, 'update_db')); | |
| 97 | 74 | |
| 98 | - $sw = 'sw.js'; | |
| 99 | - $sw_path = ABSPATH . $sw; | |
| 75 | + // Enable sidebar widgets | |
| 76 | + if ($this->settings != NULL && !empty($this->settings->publisher_id)){ | |
| 77 | + //register Taboola widget | |
| 78 | + add_action('widgets_init', | |
| 79 | + function(){ | |
| 80 | + return register_widget("WP_Widget_Taboola"); | |
| 81 | + } | |
| 82 | + ); | |
| 83 | + } | |
| 100 | 84 | |
| 101 | - $content = file_exists( $sw_path ) ? file_get_contents( $sw_path ) : ''; | |
| 85 | + //$this->should_place_tag_outside_of_content = $this->settings->out_of_content_enabled; | |
| 102 | 86 | |
| 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 ); | |
| 87 | + if (is_admin()) { | |
| 88 | + //add menu for plugin | |
| 89 | + add_action( 'admin_menu', array(&$this, 'admin_generate_menu') ); | |
| 90 | + add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 ); | |
| 91 | + }elseif ($this->settings != NULL){ | |
| 92 | + add_action('wp_head', array(&$this, 'taboola_header_loader_inject')); | |
| 93 | + if (!empty($this->settings->publisher_id_push)) { | |
| 94 | + add_action('wp_head', array(&$this, 'taboola_webpush_loader_js')); | |
| 95 | + | |
| 96 | + $sw = 'sw.js'; | |
| 97 | + $sw_path = ABSPATH . $sw; | |
| 98 | + | |
| 99 | + $content = file_exists($sw_path) ? file_get_contents($sw_path) : ''; | |
| 100 | + | |
| 101 | + if (strpos($content, $this->tpl_sw) === false) { | |
| 102 | + if (!is_writable(ABSPATH) || (file_exists($sw_path) && !is_writable($sw_path))) { | |
| 103 | + return $this->notice($this->msg_sw_error); | |
| 106 | 104 | } |
| 107 | 105 | $content = $this->tpl_sw . PHP_EOL . $content; |
| 108 | - if ( file_put_contents( $sw_path, $content ) === false ) { | |
| 109 | - return $this->notice( $this->msg_sw_error ); | |
| 106 | + if (file_put_contents($sw_path, $content) === false) { | |
| 107 | + return $this->notice($this->msg_sw_error); | |
| 110 | 108 | } |
| 111 | 109 | } |
| 112 | 110 | } |
| 113 | - add_action( 'wp_footer', [ $this, 'taboola_footer_loader_js' ] ); | |
| 111 | + add_action('wp_footer', array(&$this, 'taboola_footer_loader_js')); | |
| 112 | + add_filter('the_content', array(&$this, 'load_taboola_content')); | |
| 113 | + add_filter('the_content', array(&$this, 'load_taboola_content_mid')); | |
| 114 | + //add_filter('the_content', array(&$this, 'load_taboola_content_home')); | |
| 115 | + // add_filter('the_excerpt', array(&$this, 'load_taboola_content_home')); | |
| 116 | + // Homepage widget – capture the full page, inject once. | |
| 117 | + // Homepage widget – start buffering *before* anything else renders | |
| 118 | + add_action( 'template_redirect', [ $this, 'tb_home_buffer_start' ], 0 ); | |
| 114 | 119 | |
| 115 | - /* content widgets */ | |
| 116 | - add_filter( 'the_content', [ $this, 'load_taboola_content' ] ); | |
| 117 | - add_filter( 'the_content', [ $this, 'load_taboola_content_mid' ] ); | |
| 120 | + // Homepage widget – flush the buffer *after* literally everything else | |
| 121 | + add_action( 'shutdown', [ $this, 'tb_home_buffer_flush' ], PHP_INT_MAX ); | |
| 118 | 122 | |
| 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 | - -------------------------------------------------------------------- */ | |
| 123 | + | |
| 124 | + } | |
| 125 | + | |
| 126 | + } | |
| 127 | + | |
| 128 | + /* ------------------------------------------------------------------ | |
| 129 | + * HOMEPAGE WIDGET – full-page buffer | |
| 130 | + * ----------------------------------------------------------------*/ | |
| 144 | 131 | public function tb_home_buffer_start() { |
| 132 | + | |
| 133 | + // Use the plug-in’s own logic to decide if the home widget is enabled | |
| 145 | 134 | if ( $this->should_show_content_widget_home() ) { |
| 146 | 135 | ob_start( [ $this, 'tb_home_buffer_inject' ] ); |
| 147 | 136 | } |
| 148 | 137 | } |
| 138 | + | |
| 149 | 139 | public function tb_home_buffer_inject( $html ) { |
| 140 | + //error_log( '🟢 Taboola buffer inject ran. HTML length = ' . strlen( $html ) ); // LOG #1 | |
| 141 | + // error_log( 'TB-DEBUG ① buffer inject, html len='.strlen($html) ); | |
| 142 | + | |
| 143 | + /* | |
| 144 | + * load_taboola_content_home() already: | |
| 145 | + * – reads your saved widget-ID, placement, selector & occurrence | |
| 146 | + * – builds the HTML + JS | |
| 147 | + * – calls embed_taboola_content_location_home() to place it | |
| 148 | + */ | |
| 150 | 149 | return $this->load_taboola_content_home( $html ); |
| 151 | 150 | } |
| 152 | 151 | |
| 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 | 152 | public function tb_home_buffer_flush() { |
| 206 | 153 | if ( ob_get_length() ) { |
| 207 | 154 | echo ob_get_clean(); |
| 208 | 155 | } |
| @@ -207,12 +154,9 @@ | ||
| 207 | 154 | echo ob_get_clean(); |
| 208 | 155 | } |
| 209 | 156 | } |
| 210 | 157 | |
| 211 | - | |
| 212 | - | |
| 213 | - | |
| 214 | -function plugin_action_links($links, $file) { | |
| 158 | + function plugin_action_links($links, $file) { | |
| 215 | 159 | static $this_plugin; |
| 216 | 160 | |
| 217 | 161 | if (!$this_plugin) { |
| 218 | 162 | $this_plugin = plugin_basename(__FILE__); |
| @@ -234,9 +178,9 @@ | ||
| 234 | 178 | private function should_show_content_widget_mid(){ |
| 235 | 179 | // PC - only if v2 was installed: |
| 236 | 180 | if (!$this->is_db_updated_for_min_ver("2.0.0")) |
| 237 | 181 | return false; |
| 238 | - $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && !empty($this->settings->mid_enabled)); | |
| 182 | + $retVal1 = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->mid_enabled && trim($this->settings->mid_widget_id) != ''); | |
| 239 | 183 | return $retVal1; |
| 240 | 184 | } |
| 241 | 185 | |
| 242 | 186 | private function should_show_content_widget_home(){ |
| @@ -256,9 +200,9 @@ | ||
| 256 | 200 | } |
| 257 | 201 | |
| 258 | 202 | // Determine if a taboola widget should be added somewhere on the current page (content or sidebar) |
| 259 | 203 | 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() ; | |
| 204 | + return $this->should_show_content_widget() || $this->should_show_content_widget_mid() || $this->should_show_content_widget_home() || $this->should_show_sidebar_widget(); | |
| 261 | 205 | } |
| 262 | 206 | |
| 263 | 207 | function get_page_type(){ |
| 264 | 208 | $page_type='article'; |
| @@ -270,21 +214,15 @@ | ||
| 270 | 214 | return $page_type; |
| 271 | 215 | } |
| 272 | 216 | |
| 273 | 217 | // return the head loader script |
| 274 | - function taboola_header_loader_js() { | |
| 218 | + function taboola_header_loader_js() { | |
| 219 | + $head_string = ""; | |
| 220 | + | |
| 221 | + // Only adding the loader if a widget is going to be placed on the page. | |
| 275 | 222 | 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 | - | |
| 223 | + // PC - since these params will be inserted in 'loaderInjectionScript.js' via search and replace, | |
| 224 | + // double brackets are used to ensure that each key is a unique string. | |
| 287 | 225 | $stringParams = array( |
| 288 | 226 | '{{PUBLISHER_ID}}' => $this->settings->publisher_id, |
| 289 | 227 | '{{PAGE_TYPE}}' => $this->get_page_type(), |
| 290 | 228 | '{{WORDPRESS_VERSION}}' => get_bloginfo('version'), |
| @@ -289,18 +227,19 @@ | ||
| 289 | 227 | '{{PAGE_TYPE}}' => $this->get_page_type(), |
| 290 | 228 | '{{WORDPRESS_VERSION}}' => get_bloginfo('version'), |
| 291 | 229 | '{{PHP_VERSION}}' => phpversion(), |
| 292 | 230 | '{{PLUGIN_VERSION}}' => TABOOLA_PLUGIN_VERSION, |
| 293 | - '{{LOC_MID}}' => $mid_locations_string, | |
| 294 | - '{{LOC_HOME}}' => $this->settings->home_location_string ?? '' | |
| 231 | + '{{LOC_MID}}' => $this->settings->mid_location_string, | |
| 232 | + '{{LOC_HOME}}' => $this->settings->home_location_string | |
| 295 | 233 | ); |
| 296 | 234 | |
| 297 | 235 | $scriptWrapper = new JavaScriptWrapper("loaderInjectionScript.js",$stringParams); |
| 298 | - return $scriptWrapper->getScriptMarkupString(); | |
| 236 | + $head_string = $scriptWrapper->getScriptMarkupString(); | |
| 299 | 237 | } |
| 300 | - return ""; | |
| 238 | + return $head_string; | |
| 301 | 239 | } |
| 302 | 240 | |
| 241 | + | |
| 303 | 242 | // This function is used for the hook action, injects the header content to the <head> tag. |
| 304 | 243 | function taboola_header_loader_inject(){ |
| 305 | 244 | echo $this->taboola_header_loader_js(); |
| 306 | 245 | } |
| @@ -348,37 +287,24 @@ | ||
| 348 | 287 | return $content; |
| 349 | 288 | } |
| 350 | 289 | |
| 351 | 290 | // Mid-article-widget |
| 352 | - function load_taboola_content_mid($content) { | |
| 291 | + function load_taboola_content_mid($content) | |
| 292 | + { | |
| 293 | + $taboola_content_mid = array(); | |
| 353 | 294 | if ($this->should_show_content_widget_mid()){ |
| 354 | - $mid_widgets = json_decode($this->settings->mid_widgets ?? '[]'); | |
| 355 | 295 | |
| 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 | - ); | |
| 296 | + $secondWidgetParams = array('{{WIDGET_ID}}' => $this->settings->mid_widget_id, | |
| 297 | + '{{CONTAINER}}' => 'taboola-mid-article-thumbnails', | |
| 298 | + '{{PLACEMENT}}' => $this->settings->mid_placement); | |
| 299 | + | |
| 300 | + $secondWidgetScript = new JavaScriptWrapper("widgetInjectionScript.js",$secondWidgetParams); | |
| 301 | + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_HTML][] = "<div id='taboola-mid-article-thumbnails'></div>"; | |
| 302 | + $taboola_content_mid[TABOOLA_CONTENT_FORMAT_SCRIPT][] = $secondWidgetScript; | |
| 365 | 303 | |
| 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 | - ); | |
| 304 | + $content = $this->embed_taboola_content_location_mid($content,$taboola_content_mid,trim($this->settings->mid_location_string)); | |
| 305 | + } | |
| 371 | 306 | |
| 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 | 307 | return $content; |
| 382 | 308 | } |
| 383 | 309 | |
| 384 | 310 | // Homepage widget |
| @@ -400,33 +326,9 @@ | ||
| 400 | 326 | } |
| 401 | 327 | return $content; |
| 402 | 328 | |
| 403 | 329 | } |
| 404 | - /* ------------------------------------------------------------------ | |
| 405 | - * CATEGORY ▸ build widget & inject | |
| 406 | - * ------------------------------------------------------------------*/ | |
| 407 | -public function load_taboola_content_category( $content ) { | |
| 408 | 330 | |
| 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 | 331 | // Below-article widget |
| 430 | 332 | |
| 431 | 333 | // Extract the taboola content in the required format: |
| 432 | 334 | // String - for injecting on the servr side |
| @@ -493,41 +395,8 @@ | ||
| 493 | 395 | return $ret_val; |
| 494 | 396 | |
| 495 | 397 | } |
| 496 | 398 | |
| 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 | - /* ------------------------------------------------------------------ | |
| 508 | - * wpautop() hardening for anything injected through the_content. | |
| 509 | - * | |
| 510 | - * wpautop() pads block-level tags with blank lines, then splits the | |
| 511 | - * content on blank lines and wraps each chunk in <p>. Its <script> | |
| 512 | - * protection only runs after that split, so two things tear an injected | |
| 513 | - * <script> apart and spill its tail onto the page as reader-visible text: | |
| 514 | - * 1. a blank line anywhere in the script body, and | |
| 515 | - * 2. a block-level tag (e.g. <div>) sitting inside a JS string literal. | |
| 516 | - * Both have to be neutralised; fixing only one still breaks. | |
| 517 | - * ------------------------------------------------------------------*/ | |
| 518 | - | |
| 519 | - // Hide markup from wpautop's block-tag scan. \x3C decodes back to "<" | |
| 520 | - // when the surrounding JS string literal is evaluated. | |
| 521 | - private function js_escape_markup($markup){ | |
| 522 | - return str_replace('<', '\x3C', (string) $markup); | |
| 523 | - } | |
| 524 | - | |
| 525 | - // Collapse blank lines so wpautop has no paragraph boundary to split on. | |
| 526 | - private function wpautop_safe_script($script){ | |
| 527 | - return preg_replace('/(\R[ \t]*){2,}/', "\n", (string) $script); | |
| 528 | - } | |
| 529 | - | |
| 530 | 399 | // Below-article widget |
| 531 | 400 | // Do the actual logic of choosing where to place the taboola content. |
| 532 | 401 | function embed_taboola_content_location($content, $taboola_content){ |
| 533 | 402 | $do_default = true; |
| @@ -535,13 +404,13 @@ | ||
| 535 | 404 | // tag is placed outside of content in order to allow "read more" functionality. |
| 536 | 405 | if ($this->settings->out_of_content_enabled){ |
| 537 | 406 | |
| 538 | 407 | $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( |
| 539 | - "{{HTML}}" => $this->js_escape_markup($this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML)), | |
| 540 | - "{{SCRIPT}}" => $this->js_escape_markup($this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT))) | |
| 408 | + "{{HTML}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_HTML), | |
| 409 | + "{{SCRIPT}}" => $this->format_taboola_content($taboola_content,TABOOLA_CONTENT_FORMAT_SCRIPT)) | |
| 541 | 410 | ); |
| 542 | 411 | $scriptWrapper->appendScript("injectWidgetByMarker('tbmarker');"); |
| 543 | - $content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$this->wpautop_safe_script($scriptWrapper)."</script>"; | |
| 412 | + $content = $content."<span id='tbmarker'></span><script type='text/javascript'>".$scriptWrapper."</script>"; | |
| 544 | 413 | $do_default = false; |
| 545 | 414 | } |
| 546 | 415 | |
| 547 | 416 | // Default for below-article widget - add to the end of the content |
| @@ -553,9 +422,9 @@ | ||
| 553 | 422 | } |
| 554 | 423 | |
| 555 | 424 | // Mid-article widget |
| 556 | 425 | // Do the actual logic of choosing where to place the taboola content based on the "location" attribute |
| 557 | - function embed_taboola_content_location_mid($content, $taboola_content_mid, $location, $occurrence = 1){ | |
| 426 | + function embed_taboola_content_location_mid($content, $taboola_content_mid, $location){ | |
| 558 | 427 | $do_default = true; |
| 559 | 428 | |
| 560 | 429 | if (isset($location) && $location != ''){ |
| 561 | 430 | $first_char = substr($location,0,1); |
| @@ -567,13 +436,13 @@ | ||
| 567 | 436 | if ($full_indicator == TABOOLA_JS_INDICATOR){ |
| 568 | 437 | |
| 569 | 438 | $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR)); |
| 570 | 439 | $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( |
| 571 | - "{{HTML}}" => $this->js_escape_markup($this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_HTML)), | |
| 572 | - "{{SCRIPT}}" => $this->js_escape_markup($this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_SCRIPT))) | |
| 440 | + "{{HTML}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_HTML), | |
| 441 | + "{{SCRIPT}}" => $this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_SCRIPT)) | |
| 573 | 442 | ); |
| 574 | 443 | $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');"); |
| 575 | - $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$this->wpautop_safe_script($scriptWrapper)."</script>"; | |
| 444 | + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>"; | |
| 576 | 445 | |
| 577 | 446 | $do_default = false; |
| 578 | 447 | } |
| 579 | 448 | |
| @@ -579,14 +448,12 @@ | ||
| 579 | 448 | |
| 580 | 449 | // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm) |
| 581 | 450 | // basically it's CSS selectors like in jQuery |
| 582 | 451 | } else{ |
| 583 | - if ( ! class_exists( 'simple_html_dom' ) ) { | |
| 584 | - require_once('simple_html_dom.php'); | |
| 585 | - } | |
| 452 | + require_once('simple_html_dom.php'); | |
| 586 | 453 | |
| 587 | 454 | $html_doc = str_get_html($content); |
| 588 | - $target_location = $html_doc->find($location, ($occurrence) - 1); | |
| 455 | + $target_location = $html_doc->find($location,($this->settings->mid_location_string_occurrence)-1); | |
| 589 | 456 | |
| 590 | 457 | // if the location was found within the html content |
| 591 | 458 | if (isset($target_location) && is_object($target_location)){ |
| 592 | 459 | |
| @@ -596,8 +463,12 @@ | ||
| 596 | 463 | $do_default = false; |
| 597 | 464 | } |
| 598 | 465 | } |
| 599 | 466 | } |
| 467 | + // Default for below-article widget - add to the end of the content | |
| 468 | + // if ($do_default){ | |
| 469 | + // $content = $content.$this->format_taboola_content_mid($taboola_content_mid,TABOOLA_CONTENT_FORMAT_STRING); | |
| 470 | + // } | |
| 600 | 471 | |
| 601 | 472 | return $content; |
| 602 | 473 | } |
| 603 | 474 | |
| @@ -631,13 +502,13 @@ | ||
| 631 | 502 | if ($full_indicator == TABOOLA_JS_INDICATOR){ |
| 632 | 503 | |
| 633 | 504 | $xpath = substr($location,strlen(TABOOLA_JS_INDICATOR)); |
| 634 | 505 | $scriptWrapper = new JavaScriptWrapper("js_inject.min.js",array( |
| 635 | - "{{HTML}}" => $this->js_escape_markup($this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_HTML)), | |
| 636 | - "{{SCRIPT}}" => $this->js_escape_markup($this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_SCRIPT))) | |
| 506 | + "{{HTML}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_HTML), | |
| 507 | + "{{SCRIPT}}" => $this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_SCRIPT)) | |
| 637 | 508 | ); |
| 638 | 509 | $scriptWrapper->appendScript("injectWidgetByXpath('".$xpath."');"); |
| 639 | - $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$this->wpautop_safe_script($scriptWrapper)."</script>"; | |
| 510 | + $content = $content."<span id='tbdefault'></span><script type='text/javascript'>".$scriptWrapper."</script>"; | |
| 640 | 511 | |
| 641 | 512 | $do_default = false; |
| 642 | 513 | } |
| 643 | 514 | |
| @@ -642,12 +513,10 @@ | ||
| 642 | 513 | } |
| 643 | 514 | |
| 644 | 515 | // server side selector provided (see simple_html_dom selectors http://simplehtmldom.sourceforge.net/manual.htm) |
| 645 | 516 | // basically it's CSS selectors like in jQuery |
| 646 | - } else{ | |
| 647 | - if ( ! class_exists( 'simple_html_dom' ) ) { | |
| 517 | + } else{ | |
| 648 | 518 | require_once('simple_html_dom.php'); |
| 649 | - } | |
| 650 | 519 | |
| 651 | 520 | $html_doc = str_get_html($content); |
| 652 | 521 | $target_location = $html_doc->find($location,($this->settings->home_location_string_occurrence)-1); |
| 653 | 522 | |
| @@ -674,19 +543,12 @@ | ||
| 674 | 543 | global $current_user; |
| 675 | 544 | add_menu_page(__('Taboola','taboola_widget'), __('Taboola','taboola_widget'), 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110); |
| 676 | 545 | } |
| 677 | 546 | |
| 678 | - // Empty numeric inputs must reach MySQL as NULL, not '', or strict mode | |
| 679 | - // rejects the whole row. | |
| 680 | - private function nullable_int($value){ | |
| 681 | - return (isset($value) && trim((string) $value) !== '') ? (int) $value : null; | |
| 682 | - } | |
| 683 | - | |
| 684 | 547 | function admin_taboola_settings(){ |
| 685 | 548 | global $wpdb; |
| 686 | 549 | $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 687 | 550 | $taboola_errors = array(); |
| 688 | - $taboola_save_error = ''; | |
| 689 | 551 | if($_SERVER['REQUEST_METHOD'] == 'POST'){ |
| 690 | 552 | |
| 691 | 553 | if(trim(strip_tags($_POST['publisher_id'])) == ''){ |
| 692 | 554 | $taboola_errors[] = "Publisher ID"; |
| @@ -707,18 +569,33 @@ | ||
| 707 | 569 | } |
| 708 | 570 | } |
| 709 | 571 | |
| 710 | 572 | if(isset($_POST['mid_enabled'])) { |
| 711 | - if(isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) { | |
| 712 | - foreach ($_POST['mid_widget_id'] as $index => $widget_id) { | |
| 713 | - if(trim($widget_id) == '') { | |
| 714 | - $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Widget ID"; | |
| 715 | - } | |
| 716 | - if(trim($_POST['mid_placement'][$index]) == '') { | |
| 717 | - $taboola_errors[] = "Mid-article Widget #".($index + 1)." > Placement Name"; | |
| 718 | - } | |
| 573 | + if (trim(strip_tags($_POST['mid_widget_id'])) == '') { | |
| 574 | + $taboola_errors[] = "Mid-article > Widget ID"; | |
| 575 | + } | |
| 576 | + if (trim(strip_tags($_POST['mid_placement'])) == '') { | |
| 577 | + $taboola_errors[] = "Mid-article > Placement Name"; | |
| 578 | + } | |
| 579 | + if (trim(strip_tags($_POST['mid_location_string'])) == '') { | |
| 580 | + $taboola_errors[] = "Mid-article > CSS selector"; | |
| 581 | + } | |
| 582 | + else { | |
| 583 | + // If the 'location' WAS filled in, then... | |
| 584 | + | |
| 585 | + // 1) Check for a valid 'location': | |
| 586 | + | |
| 587 | + // Validation method has not been implemented | |
| 588 | + // mid_if (!$this->is_locaton_string_valid1($_POST['mid_location_string'])) { | |
| 589 | + // $taboola_errors[] = "Mid-article > CSS Selector (invalid value)"; | |
| 590 | + // } | |
| 591 | + | |
| 592 | + // 2) Validate the 'occurrence': | |
| 593 | + if (!$this->is_mid_location_string_occurrence_valid($_POST['mid_location_string_occurrence'])) { | |
| 594 | + $taboola_errors[] = "Mid-article > Occurance (must be >= 1)"; | |
| 719 | 595 | } |
| 720 | 596 | } |
| 597 | + | |
| 721 | 598 | } |
| 722 | 599 | |
| 723 | 600 | if(isset($_POST['home_enabled'])) { |
| 724 | 601 | if (trim(strip_tags($_POST['home_widget_id'])) == '') { |
| @@ -731,78 +608,54 @@ | ||
| 731 | 608 | if (trim(strip_tags($_POST['home_location_string'])) == '') { |
| 732 | 609 | $taboola_errors[] = "Homepage > CSS selector"; |
| 733 | 610 | } |
| 734 | 611 | else { |
| 612 | + // If the 'location' WAS filled in, then... | |
| 613 | + | |
| 614 | + // 1) Check for a valid 'location': | |
| 615 | + | |
| 616 | + // Validation method has not been implemented | |
| 617 | + // if (!$this->is_home_location_string_valid($_POST['home_location_string'])) { | |
| 618 | + // $taboola_errors[] = "Homepage > CSS Selector (invalid value)"; | |
| 619 | + // } | |
| 620 | + | |
| 621 | + // 2) Validate the 'occurrence': | |
| 735 | 622 | if (!empty($_POST['home_location_string']) && !$this->is_home_location_string_occurrence_valid($_POST['home_location_string_occurrence'])) { |
| 736 | 623 | $taboola_errors[] = "Homepage > Occurance (must be >= 1)"; |
| 737 | 624 | } |
| 738 | 625 | } |
| 739 | - } | |
| 740 | - if ( isset($_POST['category_enabled']) ) { | |
| 741 | - if ( trim(strip_tags($_POST['category_widget_id'])) == '' ) | |
| 742 | - $taboola_errors[] = "Category > Widget ID"; | |
| 743 | - if ( trim(strip_tags($_POST['category_placement'])) == '' ) | |
| 744 | - $taboola_errors[] = "Category > Placement Name"; | |
| 745 | - } | |
| 626 | + } | |
| 746 | 627 | |
| 747 | 628 | if(count($taboola_errors) == 0){ |
| 748 | - | |
| 749 | - $mid_widgets_data = array(); | |
| 750 | - if (isset($_POST['mid_widget_id']) && is_array($_POST['mid_widget_id'])) { | |
| 751 | - foreach ($_POST['mid_widget_id'] as $index => $widget_id) { | |
| 752 | - if (!empty(trim($widget_id))) { | |
| 753 | - | |
| 754 | - $location_string = 'p'; | |
| 755 | - if (isset($_POST['mid_paragraph_ui_mode'][$index]) && $_POST['mid_paragraph_ui_mode'][$index] === 'Other') { | |
| 756 | - if (isset($_POST['mid_location_string'][$index])) { | |
| 757 | - $location_string = sanitize_text_field($_POST['mid_location_string'][$index]); | |
| 758 | - } | |
| 759 | - } | |
| 760 | - | |
| 761 | - $mid_widgets_data[] = array( | |
| 762 | - 'widget_id' => sanitize_text_field($widget_id), | |
| 763 | - 'placement' => sanitize_text_field($_POST['mid_placement'][$index]), | |
| 764 | - 'location_string' => $location_string, | |
| 765 | - 'occurrence' => intval($_POST['mid_location_string_occurrence'][$index]), | |
| 766 | - ); | |
| 767 | - } | |
| 768 | - } | |
| 769 | - } | |
| 770 | - $mid_widgets_json = json_encode($mid_widgets_data); | |
| 771 | - | |
| 772 | - /* $wpdb formats every value as %s unless told otherwise, so a PHP | |
| 773 | - false or an empty string reaches MySQL as ''. Under | |
| 774 | - STRICT_TRANS_TABLES (the MySQL 8 default) '' is rejected for the | |
| 775 | - TINYINT/INT columns and the whole write is refused. Send real | |
| 776 | - integers for the flags and NULL for empty numeric fields. */ | |
| 777 | 629 | $data = array( |
| 778 | 630 | "publisher_id" => trim($_POST['publisher_id']), |
| 779 | 631 | |
| 780 | - "web_push_enabled" => isset($_POST['web_push_enabled']) ? 1 : 0, | |
| 781 | - "publisher_id_push" => $this->nullable_int($_POST['publisher_id_push'] ?? null), | |
| 632 | + "web_push_enabled" => isset($_POST['web_push_enabled']) ? true : false, | |
| 633 | + "publisher_id_push" => !empty($_POST['publisher_id_push']) ? trim($_POST['publisher_id_push']) : '', | |
| 782 | 634 | |
| 783 | - "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? 1 : 0, | |
| 635 | + "first_bc_enabled" => isset($_POST['first_bc_enabled']) ? true : false, | |
| 784 | 636 | "first_bc_widget_id" => !empty($_POST['first_bc_widget_id']) ? trim($_POST['first_bc_widget_id']) : '', |
| 785 | 637 | "first_bc_placement" => !empty($_POST['first_bc_placement']) ? trim($_POST['first_bc_placement']) : '', |
| 786 | 638 | |
| 787 | - "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? 1 : 0, | |
| 639 | + "out_of_content_enabled" => isset($_POST['out_of_content_enabled']) ? true : false, | |
| 788 | 640 | |
| 789 | - "mid_enabled" => isset($_POST['mid_enabled']) ? 1 : 0, | |
| 790 | - "mid_widgets" => $mid_widgets_json, | |
| 791 | - | |
| 792 | - "home_enabled" => isset($_POST['home_enabled']) ? 1 : 0, | |
| 641 | + "mid_enabled" => isset($_POST['mid_enabled']) ? true : false, | |
| 642 | + "mid_widget_id" => !empty($_POST['mid_widget_id']) ? trim($_POST['mid_widget_id']) : '', | |
| 643 | + "mid_placement" => !empty($_POST['mid_placement']) ? trim($_POST['mid_placement']) : '', | |
| 644 | + "mid_paragraph_ui_mode" => !empty($_POST['mid_paragraph_ui_mode']) ? trim($_POST['mid_paragraph_ui_mode']) : '', | |
| 645 | + | |
| 646 | + "mid_location_string_occurrence" => !empty($_POST['mid_location_string_occurrence']) ? $_POST['mid_location_string_occurrence'] : '', | |
| 647 | + "mid_location_string" => !empty($_POST['mid_location_string']) ? trim($_POST['mid_location_string']) : '', | |
| 648 | + | |
| 649 | + "home_enabled" => isset($_POST['home_enabled']) ? true : false, | |
| 793 | 650 | "home_widget_id" => !empty($_POST['home_widget_id']) ? trim($_POST['home_widget_id']) : '', |
| 794 | 651 | "home_placement" => !empty($_POST['home_placement']) ? trim($_POST['home_placement']) : '', |
| 795 | 652 | |
| 796 | - "home_location_string_occurrence" => $this->nullable_int($_POST['home_location_string_occurrence'] ?? null), | |
| 797 | - "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : '', | |
| 798 | - "category_enabled" => isset($_POST['category_enabled']) ? 1 : 0, | |
| 799 | - "category_widget_id" => !empty($_POST['category_widget_id']) ? trim($_POST['category_widget_id']) : '', | |
| 800 | - "category_placement" => !empty($_POST['category_placement']) ? trim($_POST['category_placement']) : '', | |
| 801 | - "category_location_string_occurrence" => $this->nullable_int($_POST['category_location_string_occurrence'] ?? null), | |
| 802 | - "category_location_string" => !empty($_POST['category_location_string']) ? trim($_POST['category_location_string']) : '', | |
| 653 | + "home_location_string_occurrence" => !empty($_POST['home_location_string_occurrence']) ? $_POST['home_location_string_occurrence'] : '', | |
| 654 | + "home_location_string" => !empty($_POST['home_location_string']) ? trim($_POST['home_location_string']) : '' | |
| 655 | + ); | |
| 803 | 656 | |
| 804 | - ); | |
| 657 | + //var_dump($settings); | |
| 805 | 658 | |
| 806 | 659 | $is_valid_nonce = false; |
| 807 | 660 | |
| 808 | 661 | if (isset( $_POST['my_plugin_nonce']) && wp_verify_nonce( $_POST['my_plugin_nonce'], 'my_plugin_update_field_action' ) ) { |
| @@ -810,20 +663,12 @@ | ||
| 810 | 663 | } |
| 811 | 664 | |
| 812 | 665 | if ($is_valid_nonce) { |
| 813 | 666 | if($settings == NULL){ |
| 814 | - $saved = $wpdb->insert($this->tbl_taboola_settings, $data); | |
| 667 | + $wpdb->insert($this->tbl_taboola_settings, $data, null, null); | |
| 815 | 668 | } else { |
| 816 | - $saved = $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id)); | |
| 669 | + $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id)); | |
| 817 | 670 | } |
| 818 | - | |
| 819 | - // update() returns 0 when nothing changed; only false is a failure. | |
| 820 | - if ($saved === false) { | |
| 821 | - $taboola_save_error = "The database rejected the write, so your changes were not saved: " | |
| 822 | - . ($wpdb->last_error !== '' ? $wpdb->last_error : 'unknown database error'); | |
| 823 | - } | |
| 824 | - } else { | |
| 825 | - $taboola_save_error = "Security check failed - the settings page had been open too long. Reload it and apply your changes again."; | |
| 826 | 671 | } |
| 827 | 672 | } |
| 828 | 673 | $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| 829 | 674 | } |
| @@ -885,9 +730,21 @@ | ||
| 885 | 730 | } |
| 886 | 731 | } |
| 887 | 732 | |
| 888 | 733 | function is_db_updated_for_min_ver($min_ver) { |
| 734 | + /** | |
| 735 | + * Checks if the DB was *previously* updated for the minimum version specified. | |
| 736 | + * $min_ver should be passed in a format like this: "2.1.0" | |
| 737 | + */ | |
| 738 | + | |
| 889 | 739 | global $wpdb; |
| 740 | + | |
| 741 | + // PC - Temporary patch for v2.1.0 | |
| 742 | + | |
| 743 | + // START patch =============================================== | |
| 744 | + // Check if the `_taboola` table exists. | |
| 745 | + // If not, then return false. | |
| 746 | + | |
| 890 | 747 | $table_name = $wpdb->prefix . "_taboola_settings"; |
| 891 | 748 | |
| 892 | 749 | if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { |
| 893 | 750 | tb_write_log("Table NOT FOUND"); |
| @@ -895,31 +752,55 @@ | ||
| 895 | 752 | } |
| 896 | 753 | else { |
| 897 | 754 | tb_write_log("Table EXISTS"); |
| 898 | 755 | } |
| 756 | + // END patch ================================================= | |
| 757 | + | |
| 758 | + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found. | |
| 899 | 759 | $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0'); |
| 900 | 760 | |
| 901 | 761 | $db_is_up_to_date = version_compare($saved_version, $min_ver, '>='); |
| 762 | + //tb_write_log($db_is_up_to_date); | |
| 902 | 763 | return $db_is_up_to_date; |
| 903 | 764 | |
| 904 | 765 | } |
| 905 | 766 | |
| 906 | 767 | function is_db_updated_for_current_ver() { |
| 768 | + /** | |
| 769 | + * DEPRECATED | |
| 770 | + * Checks if the DB was *previously* updated for the version currently loaded. | |
| 771 | + */ | |
| 907 | 772 | global $wpdb; |
| 773 | + | |
| 774 | + // Check the saved version in wp_options. Default to '1.0.0' if the option is not found. | |
| 908 | 775 | $saved_version = get_option(TABOOLA_OPTION_NAME, '1.0.0'); |
| 776 | + | |
| 777 | + // Get the currently loaded plugin version. | |
| 909 | 778 | $plugin_version = $this->get_loaded_plugin_version(); |
| 779 | + | |
| 910 | 780 | $db_is_up_to_date = version_compare($saved_version, $plugin_version, '>='); |
| 781 | + //tb_write_log($db_is_up_to_date); | |
| 911 | 782 | return $db_is_up_to_date; |
| 783 | + | |
| 912 | 784 | } |
| 913 | 785 | |
| 914 | 786 | function get_loaded_plugin_version() { |
| 915 | - $plugin_data = get_plugin_data( __FILE__ ); | |
| 787 | + | |
| 788 | + $plugin_data = get_plugin_data( __FILE__ ); // Can be invoked from Admin page only | |
| 916 | 789 | $loaded_plugin_version = $plugin_data['Version']; |
| 917 | 790 | return $loaded_plugin_version; |
| 791 | + | |
| 918 | 792 | } |
| 919 | 793 | |
| 920 | 794 | function save_taboola_version($min_ver) { |
| 795 | + /** | |
| 796 | + * Checks for the saved version in wp_options. | |
| 797 | + * If not found - adds it. Else - updates it. | |
| 798 | + */ | |
| 799 | + | |
| 800 | + | |
| 921 | 801 | tb_write_log("SAVING NEW MIN VERSION: " . $min_ver); // PC |
| 802 | + | |
| 922 | 803 | $saved_version = get_option(TABOOLA_OPTION_NAME, ''); |
| 923 | 804 | if ($saved_version == '') { |
| 924 | 805 | add_option(TABOOLA_OPTION_NAME, $min_ver); |
| 925 | 806 | } |
| @@ -928,9 +809,12 @@ | ||
| 928 | 809 | } |
| 929 | 810 | } |
| 930 | 811 | |
| 931 | 812 | function update_db(){ |
| 813 | + | |
| 814 | + // If we are up to date, then skip this method... | |
| 932 | 815 | if ($this->is_db_updated_for_min_ver(TABOOLA_MIN_VER)) { |
| 816 | + //tb_write_log("All up to date!"); | |
| 933 | 817 | return; |
| 934 | 818 | } |
| 935 | 819 | |
| 936 | 820 | $this->save_taboola_version(TABOOLA_MIN_VER); |
| @@ -937,17 +821,24 @@ | ||
| 937 | 821 | |
| 938 | 822 | global $wpdb; |
| 939 | 823 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 940 | 824 | |
| 941 | - $charset_collate = ''; | |
| 942 | - if ( ! empty( $wpdb->charset ) ) { | |
| 943 | - $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; | |
| 825 | + | |
| 826 | + // Handling for older MySQL versions | |
| 827 | + if (function_exists('mysql_get_server_info') && version_compare(mysql_get_server_info(), '4.1.0', '>=')) { | |
| 828 | + if (!empty($wpdb->charset)) | |
| 829 | + $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; | |
| 830 | + if (!empty($wpdb->collate)) | |
| 831 | + $charset_collate .= " COLLATE $wpdb->collate"; | |
| 944 | 832 | } |
| 945 | - if ( ! empty( $wpdb->collate ) ) { | |
| 946 | - $charset_collate .= " COLLATE {$wpdb->collate}"; | |
| 947 | - } | |
| 948 | 833 | |
| 834 | + // Check if this is an upgrade from v1 | |
| 949 | 835 | $is_upgrade_from_v1 = $this->is_upgrade_from_v1(); |
| 836 | + | |
| 837 | + // tb_write_log("Is upgrade from v1: " . ($is_upgrade_from_v1 ? 'true' : 'false')); | |
| 838 | + | |
| 839 | + | |
| 840 | + //settings table structure | |
| 950 | 841 | $sql_table_settings = " |
| 951 | 842 | CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` ( |
| 952 | 843 | `id` INT NOT NULL AUTO_INCREMENT , |
| 953 | 844 | `publisher_id` VARCHAR(255) DEFAULT NULL, |
| @@ -955,23 +846,31 @@ | ||
| 955 | 846 | `publisher_id_push` INT(15) DEFAULT NULL, |
| 956 | 847 | `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 957 | 848 | `first_bc_widget_id` VARCHAR(255) DEFAULT NULL, |
| 958 | 849 | `first_bc_placement` VARCHAR(255) DEFAULT " . ($is_upgrade_from_v1 ? "'below-article'" : "NULL") .", |
| 850 | + `first_bc_custom_css` TEXT DEFAULT NULL, | |
| 851 | + `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, | |
| 852 | + `second_bc_widget_id` VARCHAR(255) DEFAULT NULL, | |
| 853 | + `second_bc_custom_css` TEXT DEFAULT NULL, | |
| 854 | + `location_string` TEXT DEFAULT NULL, | |
| 855 | + `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, | |
| 856 | + `mid_widget_id` VARCHAR(255) DEFAULT NULL, | |
| 857 | + `mid_placement` VARCHAR(255) DEFAULT NULL, | |
| 959 | 858 | `out_of_content_enabled` TINYINT(1) NOT NULL DEFAULT TRUE, |
| 960 | - `mid_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, | |
| 961 | - `mid_widgets` TEXT DEFAULT NULL, | |
| 859 | + `mid_location_string` TEXT DEFAULT NULL, | |
| 860 | + `mid_location_string_occurrence` SMALLINT DEFAULT NULL, | |
| 861 | + `mid_paragraph_ui_mode` VARCHAR(255) DEFAULT NULL, | |
| 962 | 862 | `home_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, |
| 963 | 863 | `home_widget_id` VARCHAR(255) DEFAULT NULL, |
| 964 | 864 | `home_placement` VARCHAR(255) DEFAULT NULL, |
| 965 | 865 | `home_location_string` TEXT DEFAULT NULL, |
| 966 | 866 | `home_location_string_occurrence` SMALLINT DEFAULT NULL, |
| 967 | - `category_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, | |
| 968 | - `category_widget_id` VARCHAR(255) DEFAULT NULL, | |
| 969 | - `category_placement` VARCHAR(255) DEFAULT NULL, | |
| 970 | - `category_location_string` TEXT DEFAULT NULL, | |
| 971 | - `category_location_string_occurrence` SMALLINT DEFAULT NULL, | |
| 972 | 867 | PRIMARY KEY (`id`) |
| 973 | - ) $charset_collate;"; | |
| 868 | + )" . $charset_collate . ";"; | |
| 869 | + | |
| 870 | + // tb_write_log($sql_table_settings); | |
| 871 | + | |
| 872 | + // create/update the table | |
| 974 | 873 | dbDelta($sql_table_settings); |
| 975 | 874 | } |
| 976 | 875 | } |
| 977 | 876 | } |
| @@ -978,11 +877,19 @@ | ||
| 978 | 877 | |
| 979 | 878 | global $taboolaWP; |
| 980 | 879 | $taboolaWP = new TaboolaWP(); |
| 981 | 880 | |
| 881 | +/* | |
| 882 | +A few utility methods for logging | |
| 883 | +*/ | |
| 884 | + | |
| 885 | +// Writes debugging messages to 'logs/debug.log'. | |
| 886 | +// The exact file location depends on *where* you trigger the function. | |
| 887 | +// In this script, we trigger the function from the admin dashboard. | |
| 888 | +// So the log file is located under 'wp-admin/logs'. | |
| 982 | 889 | function tb_write_log($log_msg) |
| 983 | 890 | { |
| 984 | - if (!TABOOLA_DEBUG_MODE) | |
| 891 | + if (!TABOOLA_DEBUG_MODE) // If not debug mode, do nothing | |
| 985 | 892 | return; |
| 986 | 893 | |
| 987 | 894 | $log_filename = "logs"; |
| 988 | 895 | if (!file_exists($log_filename)) |
| @@ -990,8 +897,9 @@ | ||
| 990 | 897 | mkdir($log_filename, 0777, true); |
| 991 | 898 | } |
| 992 | 899 | $log_file_data = $log_filename.'/debug.log'; |
| 993 | 900 | |
| 901 | + // Add date/time | |
| 994 | 902 | $date = date('Y-m-d H:i:s'); |
| 995 | 903 | $log_msg_with_date = $date." : ".$log_msg; |
| 996 | 904 | |
| 997 | 905 | file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND); |
| @@ -996,8 +904,9 @@ | ||
| 996 | 904 | |
| 997 | 905 | file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND); |
| 998 | 906 | } |
| 999 | 907 | |
| 908 | +// Write to console | |
| 1000 | 909 | function tb_console_log( $data ){ |
| 1001 | 910 | |
| 1002 | 911 | If(wp_get_environment_type() === 'development') { |
| 1003 | 912 | |
| @@ -1007,8 +916,9 @@ | ||
| 1007 | 916 | |
| 1008 | 917 | } |
| 1009 | 918 | } |
| 1010 | 919 | |
| 920 | +// Write to webpage and stop | |
| 1011 | 921 | function tb_print_to_page($data) { |
| 1012 | 922 | If(wp_get_environment_type() === 'development') { |
| 1013 | 923 | print_r($data); |
| 1014 | 924 | die; |
| @@ -1013,17 +923,19 @@ | ||
| 1013 | 923 | print_r($data); |
| 1014 | 924 | die; |
| 1015 | 925 | } |
| 1016 | 926 | } |
| 927 | +// | |
| 1017 | 928 | |
| 929 | +// Enqueue js_inject.min.js script for XPath injection | |
| 1018 | 930 | function enqueue_taboola_scripts() { |
| 1019 | 931 | wp_register_script( |
| 1020 | 932 | 'taboola-injector', |
| 1021 | 933 | plugins_url('js/js_inject.min.js', __FILE__), |
| 1022 | - array(), | |
| 1023 | - null, | |
| 1024 | - true | |
| 934 | + array(), // No dependencies | |
| 935 | + null, // No version specified | |
| 936 | + true // Load in footer | |
| 1025 | 937 | ); |
| 1026 | 938 | |
| 1027 | 939 | wp_enqueue_script('taboola-injector'); |
| 1028 | 940 | } |
| 1029 | -add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts'); | |
| 941 | +add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts'); | |