| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Plugin Name: Taboola |
| 4 | 4 | * Plugin URI: http://taboola.com |
| 5 | 5 | * Description: Taboola |
| 6 | - * Version: 1.0 | |
| 6 | + * Version: 1.0.2 | |
| 7 | 7 | * Author: Taboola |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | include_once('widget.php'); |
| @@ -11,18 +11,23 @@ | ||
| 11 | 11 | |
| 12 | 12 | if (!class_exists('TaboolaWP')) { |
| 13 | 13 | class TaboolaWP |
| 14 | 14 | { |
| 15 | - | |
| 16 | 15 | //save internal data |
| 17 | 16 | public $data = array(); |
| 17 | + public $_is_widget_on_page; | |
| 18 | + public $_is_head_script_loaded = false; | |
| 18 | 19 | |
| 19 | 20 | function TaboolaWP() |
| 20 | 21 | { |
| 21 | 22 | global $wpdb; |
| 23 | + | |
| 22 | 24 | //initialize plugin constant |
| 23 | 25 | DEFINE('TaboolaWP', true); |
| 24 | 26 | |
| 27 | + $this->_is_widget_on_page = false; | |
| 28 | + $this->_is_head_script_loaded = false; | |
| 29 | + | |
| 25 | 30 | $this->plugin_name = plugin_basename(__FILE__); |
| 26 | 31 | $this->plugin_directory = plugin_dir_path(__FILE__); |
| 27 | 32 | $this->plugin_url = trailingslashit(WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__))); |
| 28 | 33 | $this->settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); |
| @@ -31,16 +36,15 @@ | ||
| 31 | 36 | |
| 32 | 37 | //activation function |
| 33 | 38 | register_activation_hook($this->plugin_name, array(&$this, 'activate')); |
| 34 | 39 | |
| 35 | - | |
| 36 | - // disable sidebar widget | |
| 37 | - /*if($this->settings != NULL && !empty($this->settings->publisher_id)){ | |
| 40 | + // Enable sidebar widgets | |
| 41 | + if($this->settings != NULL && !empty($this->settings->publisher_id)){ | |
| 38 | 42 | //register Taboola widget |
| 39 | 43 | add_action('widgets_init', |
| 40 | 44 | create_function('', 'return register_widget("WP_Widget_Taboola");') |
| 41 | 45 | ); |
| 42 | - }*/ | |
| 46 | + } | |
| 43 | 47 | |
| 44 | 48 | if (is_admin()) { |
| 45 | 49 | //add menu for plugin |
| 46 | 50 | add_action( 'admin_menu', array(&$this, 'admin_generate_menu') ); |
| @@ -46,14 +50,18 @@ | ||
| 46 | 50 | add_action( 'admin_menu', array(&$this, 'admin_generate_menu') ); |
| 47 | 51 | add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 ); |
| 48 | 52 | } else { |
| 49 | 53 | if($this->settings != NULL){ |
| 50 | - add_action('wp_head', array(&$this, 'taboola_header_loader_js')); | |
| 54 | + add_action('wp_head', array(&$this, 'taboola_header_loader_inject')); | |
| 51 | 55 | add_action('wp_footer', array(&$this, 'taboola_footer_loader_js')); |
| 52 | 56 | add_filter('the_content', array(&$this, 'load_taboola_content')); |
| 53 | 57 | } |
| 54 | 58 | } |
| 55 | 59 | } |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 56 | 64 | function plugin_action_links($links, $file) { |
| 57 | 65 | static $this_plugin; |
| 58 | 66 | |
| 59 | 67 | if (!$this_plugin) { |
| @@ -66,41 +74,90 @@ | ||
| 66 | 74 | } |
| 67 | 75 | |
| 68 | 76 | return $links; |
| 69 | 77 | } |
| 78 | + | |
| 79 | + private function should_show_content_widget(){ | |
| 80 | + $retVal = ((trim($this->settings->publisher_id) != '') && is_single() && $this->settings->first_bc_enabled && trim($this->settings->first_bc_widget_id) != ''); | |
| 81 | + return $retVal; | |
| 82 | + } | |
| 83 | + | |
| 84 | + private function should_show_sidebar_widget(){ | |
| 85 | + $retVal = ((trim($this->settings->publisher_id) != '') && is_active_widget( false, false, TABOOLA_WIDGET_BASE_ID, true )); | |
| 86 | + return $retVal; | |
| 87 | + } | |
| 88 | + | |
| 89 | + // Determine if a taboola widget should be added somewhere on the current page (content or sidebar) | |
| 90 | + function is_widget_on_page(){ | |
| 91 | + return $this->should_show_content_widget() || $this->should_show_sidebar_widget(); | |
| 92 | + } | |
| 93 | + | |
| 94 | + function get_page_type(){ | |
| 95 | + | |
| 96 | + $page_type='article'; | |
| 97 | + if (is_front_page()){ | |
| 98 | + $page_type='home'; | |
| 99 | + }else if (is_category() || is_archive() || is_search()){ | |
| 100 | + $page_type='category'; | |
| 101 | + } | |
| 102 | + return $page_type; | |
| 103 | + } | |
| 104 | + | |
| 105 | + // return the head loader script | |
| 70 | 106 | function taboola_header_loader_js() { |
| 71 | - $script1_content = str_replace('{{PUBLISHER_ID}}', $this->settings->publisher_id, file_get_contents($this->plugin_directory.'js/script1.js')); | |
| 72 | - echo '<script type="text/javascript">'.$script1_content.'</script>'; | |
| 107 | + $head_string = ""; | |
| 108 | + | |
| 109 | + // Only adding Script1 if a widget is going to be placed on the page. | |
| 110 | + if ($this->is_widget_on_page()){ | |
| 111 | + | |
| 112 | + $script1_content = str_replace('{{PUBLISHER_ID}}', $this->settings->publisher_id, file_get_contents($this->plugin_directory.'js/script1.js')); | |
| 113 | + $script1_content = str_replace('{{PAGE_TYPE}}',$this->get_page_type(),$script1_content); | |
| 114 | + | |
| 115 | + $head_string = '<script type="text/javascript">'.$script1_content.'</script>'; | |
| 116 | + } | |
| 117 | + | |
| 118 | + return $head_string; | |
| 73 | 119 | } |
| 120 | + | |
| 121 | + | |
| 122 | + // This function is used for the hook action, injects the header content to the <head> tag. | |
| 123 | + function taboola_header_loader_inject(){ | |
| 124 | + | |
| 125 | + echo $this->taboola_header_loader_js(); | |
| 126 | + } | |
| 127 | + | |
| 128 | + | |
| 74 | 129 | function taboola_footer_loader_js() { |
| 75 | - $script3_content = file_get_contents($this->plugin_directory.'js/script3.js'); | |
| 76 | - echo '<script type="text/javascript">'.$script3_content.'</script>'; | |
| 130 | + | |
| 131 | + // Only adding Script1 if a widget is going to be placed on the page. | |
| 132 | + if ( $this->is_widget_on_page() ){ | |
| 133 | + $script3_content = file_get_contents($this->plugin_directory.'js/script3.js'); | |
| 134 | + echo '<script type="text/javascript">'.$script3_content.'</script>'; | |
| 135 | + } | |
| 77 | 136 | } |
| 78 | 137 | |
| 79 | 138 | function load_taboola_content($content) |
| 80 | 139 | { |
| 81 | - if (trim($this->settings->publisher_id) == ''){ | |
| 82 | - return $content; | |
| 83 | - } | |
| 84 | - if (is_single()) { | |
| 85 | 140 | |
| 86 | - if($this->settings->first_bc_enabled && trim($this->settings->first_bc_widget_id) != ''){ | |
| 87 | - $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->first_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js')); | |
| 88 | - $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article', $script2_content); | |
| 89 | - $script2_content = str_replace('{{PLACEMENT}}', 'below-article', $script2_content); | |
| 141 | + if ($this->should_show_content_widget()){ | |
| 90 | 142 | |
| 143 | + $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->first_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js')); | |
| 144 | + $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article', $script2_content); | |
| 145 | + $script2_content = str_replace('{{PLACEMENT}}', 'below-article', $script2_content); | |
| 146 | + | |
| 147 | + $content = $content . | |
| 148 | + '<div id="taboola-below-article"></div>' . | |
| 149 | + '<script type="text/javascript">'.$script2_content.'</script>'; | |
| 150 | + | |
| 151 | + // Adding the 2nd widget if needed | |
| 152 | + if($this->settings->second_bc_enabled && trim($this->settings->second_bc_widget_id) != ''){ | |
| 153 | + $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->second_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js')); | |
| 154 | + $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article-second', $script2_content); | |
| 155 | + $script2_content = str_replace('{{PLACEMENT}}', 'below-article-2nd', $script2_content); | |
| 156 | + | |
| 91 | 157 | $content = $content . |
| 92 | - '<div id="taboola-below-article" style="'.$this->settings->first_bc_custom_css.'"></div>' . | |
| 158 | + '<div id="taboola-below-article-second"></div>' . | |
| 93 | 159 | '<script type="text/javascript">'.$script2_content.'</script>'; |
| 94 | - if($this->settings->second_bc_enabled && trim($this->settings->second_bc_widget_id) != ''){ | |
| 95 | - $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->second_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js')); | |
| 96 | - $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article-second', $script2_content); | |
| 97 | - $script2_content = str_replace('{{PLACEMENT}}', 'below-article-2nd', $script2_content); | |
| 98 | - | |
| 99 | - $content = $content . | |
| 100 | - '<div id="taboola-below-article-second" style="'.$this->settings->second_bc_custom_css.'"></div>' . | |
| 101 | - '<script type="text/javascript">'.$script2_content.'</script>'; | |
| 102 | - } | |
| 103 | 160 | } |
| 104 | 161 | } |
| 105 | 162 | |
| 106 | 163 | return $content; |
| @@ -130,12 +187,10 @@ | ||
| 130 | 187 | $data = array( |
| 131 | 188 | "publisher_id" => trim($_POST['publisher_id']), |
| 132 | 189 | "first_bc_enabled" => $_POST['first_bc_enabled'] == 'on' ? true : false, |
| 133 | 190 | "first_bc_widget_id" => trim($_POST['first_bc_widget_id']), |
| 134 | - "first_bc_custom_css" => trim($_POST['first_bc_custom_css']), | |
| 135 | 191 | "second_bc_enabled" => $_POST['second_bc_enabled'] == 'on' ? true : false, |
| 136 | 192 | "second_bc_widget_id" => trim($_POST['second_bc_widget_id']), |
| 137 | - "second_bc_custom_css" => trim($_POST['second_bc_custom_css']) | |
| 138 | 193 | ); |
| 139 | 194 | //var_dump($settings); |
| 140 | 195 | if($settings == NULL){ |
| 141 | 196 | $wpdb->insert($this->tbl_taboola_settings, $data, null, null); |