| @@ -2,13 +2,13 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Plugin Name: Taboola |
| 4 | 4 | * Plugin URI: https://developers.taboola.com/web-integrations/docs/wordpress-plugin |
| 5 | 5 | * Description: Taboola |
| 6 | - * Version: 2.2.2 | |
| 6 | + * Version: 2.2.3 | |
| 7 | 7 | * Author: Taboola |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -define ("TABOOLA_PLUGIN_VERSION","2.2.2"); // => UPDATE THIS FOR *EVERY* RELEASE (USED FOR TRACKING) | |
| 10 | +define ("TABOOLA_PLUGIN_VERSION","2.2.3"); // => UPDATE THIS FOR *EVERY* RELEASE (USED FOR TRACKING) | |
| 11 | 11 | define ("TABOOLA_MIN_VER","2.2.2"); // => UPDATE THIS *ONLY* IF THIS RELEASE HAS *DB CHANGES* |
| 12 | 12 | define ("TABOOLA_DEBUG_MODE", false); // => SET THIS TO 'FALSE' FOR *EVERY* RELEASE (USED TO SUPRESS DEBUGGING LOGS) |
| 13 | 13 | |
| 14 | 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'. |
| @@ -110,12 +110,52 @@ | ||
| 110 | 110 | } |
| 111 | 111 | add_action('wp_footer', array(&$this, 'taboola_footer_loader_js')); |
| 112 | 112 | add_filter('the_content', array(&$this, 'load_taboola_content')); |
| 113 | 113 | add_filter('the_content', array(&$this, 'load_taboola_content_mid')); |
| 114 | - add_filter('the_content', array(&$this, 'load_taboola_content_home')); | |
| 115 | - } | |
| 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 ); | |
| 119 | + | |
| 120 | + // Homepage widget – flush the buffer *after* literally everything else | |
| 121 | + add_action( 'shutdown', [ $this, 'tb_home_buffer_flush' ], PHP_INT_MAX ); | |
| 122 | + | |
| 123 | + | |
| 124 | + } | |
| 125 | + | |
| 116 | 126 | } |
| 127 | + | |
| 128 | + /* ------------------------------------------------------------------ | |
| 129 | + * HOMEPAGE WIDGET – full-page buffer | |
| 130 | + * ----------------------------------------------------------------*/ | |
| 131 | +public function tb_home_buffer_start() { | |
| 132 | + | |
| 133 | + // Use the plug-in’s own logic to decide if the home widget is enabled | |
| 134 | + if ( $this->should_show_content_widget_home() ) { | |
| 135 | + ob_start( [ $this, 'tb_home_buffer_inject' ] ); | |
| 136 | + } | |
| 137 | +} | |
| 117 | 138 | |
| 139 | +public function tb_home_buffer_inject( $html ) { | |
| 140 | + //error_log( '🟢 Taboola buffer inject ran. HTML length = ' . strlen( $html ) ); // LOG #1 | |
| 141 | + // error_log( 'TB-DEBUG ① buffer inject, html len='.strlen($html) ); | |
| 142 | + | |
| 143 | + /* | |
| 144 | + * load_taboola_content_home() already: | |
| 145 | + * – reads your saved widget-ID, placement, selector & occurrence | |
| 146 | + * – builds the HTML + JS | |
| 147 | + * – calls embed_taboola_content_location_home() to place it | |
| 148 | + */ | |
| 149 | + return $this->load_taboola_content_home( $html ); | |
| 150 | +} | |
| 151 | + | |
| 152 | +public function tb_home_buffer_flush() { | |
| 153 | + if ( ob_get_length() ) { | |
| 154 | + echo ob_get_clean(); | |
| 155 | + } | |
| 156 | +} | |
| 157 | + | |
| 118 | 158 | function plugin_action_links($links, $file) { |
| 119 | 159 | static $this_plugin; |
| 120 | 160 | |
| 121 | 161 | if (!$this_plugin) { |
| @@ -144,8 +184,11 @@ | ||
| 144 | 184 | } |
| 145 | 185 | |
| 146 | 186 | private function should_show_content_widget_home(){ |
| 147 | 187 | // PC - only if v2 was installed: |
| 188 | + | |
| 189 | + //error_log( print_r( $this->settings, true ) ); // TEMP line | |
| 190 | + | |
| 148 | 191 | if (!$this->is_db_updated_for_min_ver("2.0.0")) |
| 149 | 192 | return false; |
| 150 | 193 | $retVal2 = ((trim($this->settings->publisher_id) != '') && (is_front_page() || is_home()) && $this->settings->home_enabled && trim($this->settings->home_widget_id) != ''); |
| 151 | 194 | return $retVal2; |
| @@ -266,9 +309,9 @@ | ||
| 266 | 309 | |
| 267 | 310 | // Homepage widget |
| 268 | 311 | function load_taboola_content_home($content) |
| 269 | 312 | { |
| 270 | - | |
| 313 | + //error_log( 'TB-DEBUG ② entered load_taboola_content_home' ); | |
| 271 | 314 | $taboola_content_home = array(); |
| 272 | 315 | if ($this->should_show_content_widget_home()){ |
| 273 | 316 | |
| 274 | 317 | $homeWidgetParams = array('{{WIDGET_ID}}' => $this->settings->home_widget_id, |
| @@ -432,8 +475,22 @@ | ||
| 432 | 475 | // Homepage widget |
| 433 | 476 | // Do the actual logic of choosing where to place the taboola content based on the "location" attribute |
| 434 | 477 | function embed_taboola_content_location_home($content, $taboola_content_home, $location){ |
| 435 | 478 | |
| 479 | + | |
| 480 | + // error_log( 'TB-DEBUG ③ raw selector string: [' . $location . ']' ); | |
| 481 | + | |
| 482 | + // load parser | |
| 483 | + //require_once plugin_dir_path( __FILE__ ) . 'simple_html_dom.php'; | |
| 484 | + // $html_doc = str_get_html( $content ); | |
| 485 | + | |
| 486 | + // occurrence: settings is 1-based, find() wants 0-based | |
| 487 | + // $occurrence = max( 1, intval( $this->settings->home_location_string_occurrence ) ); | |
| 488 | + // $node = $html_doc->find( $location, $occurrence - 1 ); | |
| 489 | + // $found = is_object( $node ) ? 1 : 0; | |
| 490 | + | |
| 491 | + // error_log( "TB-DEBUG ③ found {$found} matching node(s) for selector [{$location}]" ); | |
| 492 | + // ──────────────────────────── | |
| 436 | 493 | $do_default = true; |
| 437 | 494 | |
| 438 | 495 | if (isset($location) && $location != ''){ |
| 439 | 496 | $first_char = substr($location,0,1); |
| @@ -473,12 +530,13 @@ | ||
| 473 | 530 | } |
| 474 | 531 | } |
| 475 | 532 | } |
| 476 | 533 | // Default for below-article widget - add to the end of the content |
| 477 | - // if ($do_default){ | |
| 478 | - // $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING); | |
| 479 | - // } | |
| 534 | + if ($do_default){ | |
| 535 | + $content = $content.$this->format_taboola_content_home($taboola_content_home,TABOOLA_CONTENT_FORMAT_STRING); | |
| 536 | + } | |
| 480 | 537 | return $content; |
| 538 | + | |
| 481 | 539 | |
| 482 | 540 | } |
| 483 | 541 | |
| 484 | 542 | function admin_generate_menu(){ |
| @@ -829,8 +887,11 @@ | ||
| 829 | 887 | // In this script, we trigger the function from the admin dashboard. |
| 830 | 888 | // So the log file is located under 'wp-admin/logs'. |
| 831 | 889 | function tb_write_log($log_msg) |
| 832 | 890 | { |
| 891 | + if (!TABOOLA_DEBUG_MODE) // If not debug mode, do nothing | |
| 892 | + return; | |
| 893 | + | |
| 833 | 894 | $log_filename = "logs"; |
| 834 | 895 | if (!file_exists($log_filename)) |
| 835 | 896 | { |
| 836 | 897 | mkdir($log_filename, 0777, true); |
| @@ -839,12 +900,10 @@ | ||
| 839 | 900 | |
| 840 | 901 | // Add date/time |
| 841 | 902 | $date = date('Y-m-d H:i:s'); |
| 842 | 903 | $log_msg_with_date = $date." : ".$log_msg; |
| 843 | - | |
| 844 | - if (TABOOLA_DEBUG_MODE) | |
| 845 | - file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND); | |
| 846 | - | |
| 904 | + | |
| 905 | + file_put_contents($log_file_data, $log_msg_with_date . "\n", FILE_APPEND); | |
| 847 | 906 | } |
| 848 | 907 | |
| 849 | 908 | // Write to console |
| 850 | 909 | function tb_console_log( $data ){ |
| @@ -864,6 +923,19 @@ | ||
| 864 | 923 | print_r($data); |
| 865 | 924 | die; |
| 866 | 925 | } |
| 867 | 926 | } |
| 927 | +// | |
| 868 | 928 | |
| 869 | -// | |
| 929 | +// Enqueue js_inject.min.js script for XPath injection | |
| 930 | +function enqueue_taboola_scripts() { | |
| 931 | + wp_register_script( | |
| 932 | + 'taboola-injector', | |
| 933 | + plugins_url('js/js_inject.min.js', __FILE__), | |
| 934 | + array(), // No dependencies | |
| 935 | + null, // No version specified | |
| 936 | + true // Load in footer | |
| 937 | + ); | |
| 938 | + | |
| 939 | + wp_enqueue_script('taboola-injector'); | |
| 940 | +} | |
| 941 | +add_action('wp_enqueue_scripts', 'enqueue_taboola_scripts'); | |