plugin_name = plugin_basename(__FILE__); $this->plugin_directory = plugin_dir_path(__FILE__); $this->plugin_url = trailingslashit(WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__))); $this->settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); $this->tbl_taboola_settings = $wpdb->prefix . '_taboola_settings'; //activation function register_activation_hook($this->plugin_name, array(&$this, 'activate')); // disable sidebar widget /*if($this->settings != NULL && !empty($this->settings->publisher_id)){ //register Taboola widget add_action('widgets_init', create_function('', 'return register_widget("WP_Widget_Taboola");') ); }*/ if (is_admin()) { //add menu for plugin add_action( 'admin_menu', array(&$this, 'admin_generate_menu') ); add_filter('plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 ); } else { if($this->settings != NULL){ add_action('wp_head', array(&$this, 'taboola_header_loader_js')); add_action('wp_footer', array(&$this, 'taboola_footer_loader_js')); add_filter('the_content', array(&$this, 'load_taboola_content')); } } } function plugin_action_links($links, $file) { static $this_plugin; if (!$this_plugin) { $this_plugin = plugin_basename(__FILE__); } if ($file == $this_plugin) { $settings_link = 'Settings'; array_unshift($links, $settings_link); } return $links; } function taboola_header_loader_js() { $script1_content = str_replace('{{PUBLISHER_ID}}', $this->settings->publisher_id, file_get_contents($this->plugin_directory.'js/script1.js')); echo ''; } function taboola_footer_loader_js() { $script3_content = file_get_contents($this->plugin_directory.'js/script3.js'); echo ''; } function load_taboola_content($content) { if (trim($this->settings->publisher_id) == ''){ return $content; } if (is_single()) { if($this->settings->first_bc_enabled && trim($this->settings->first_bc_widget_id) != ''){ $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->first_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js')); $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article', $script2_content); $script2_content = str_replace('{{PLACEMENT}}', 'below-article', $script2_content); $content = $content . '
' . ''; if($this->settings->second_bc_enabled && trim($this->settings->second_bc_widget_id) != ''){ $script2_content = str_replace('{{WIDGET_ID}}', $this->settings->second_bc_widget_id, file_get_contents($this->plugin_directory.'js/script2.js')); $script2_content = str_replace('{{CONTAINER}}', 'taboola-below-article-second', $script2_content); $script2_content = str_replace('{{PLACEMENT}}', 'below-article-2nd', $script2_content); $content = $content . '' . ''; } } } return $content; } function admin_generate_menu(){ global $current_user; add_menu_page('Taboola', 'Taboola', 'manage_options', 'taboola_widget', array(&$this, 'admin_taboola_settings'), $this->plugin_url.'img/taboola_icon.png', 110); } function admin_taboola_settings(){ global $wpdb; $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); $taboola_errors = array(); if($_SERVER['REQUEST_METHOD'] == 'POST'){ if(trim($_POST['publisher_id']) == ''){ $taboola_errors[] = "Please add a 'Publisher ID' in order to apply changes to your widgets"; } if(($_POST['first_bc_enabled'] == 'on' && trim($_POST['first_bc_widget_id']) == '') || ($_POST['second_bc_enabled'] == 'on' && trim($_POST['second_bc_widget_id']) == '') ){ $taboola_errors[] = "Please add a 'Widget ID' in order to apply changes to your widgets"; } if(count($taboola_errors) == 0){ $data = array( "publisher_id" => trim($_POST['publisher_id']), "first_bc_enabled" => $_POST['first_bc_enabled'] == 'on' ? true : false, "first_bc_widget_id" => trim($_POST['first_bc_widget_id']), "first_bc_custom_css" => trim($_POST['first_bc_custom_css']), "second_bc_enabled" => $_POST['second_bc_enabled'] == 'on' ? true : false, "second_bc_widget_id" => trim($_POST['second_bc_widget_id']), "second_bc_custom_css" => trim($_POST['second_bc_custom_css']) ); //var_dump($settings); if($settings == NULL){ $wpdb->insert($this->tbl_taboola_settings, $data, null, null); } else { $wpdb->update($this->tbl_taboola_settings, $data, array('id' => $settings->id)); } } $settings = $wpdb->get_row("select * from ".$wpdb->prefix."_taboola_settings limit 1"); } include_once('settings.php'); } function activate(){ global $wpdb; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); $settings_table = $this->tbl_taboola_settings; //check mysql version if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) { if (!empty($wpdb->charset)) $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; if (!empty($wpdb->collate)) $charset_collate .= " COLLATE $wpdb->collate"; } //settings table structure $sql_table_settings = " CREATE TABLE `" . $wpdb->prefix . "_taboola_settings` ( `id` INT NOT NULL AUTO_INCREMENT , `publisher_id` VARCHAR(255) DEFAULT NULL, `first_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, `first_bc_widget_id` VARCHAR(255) DEFAULT NULL, `first_bc_custom_css` TEXT DEFAULT NULL, `second_bc_enabled` TINYINT(1) NOT NULL DEFAULT FALSE, `second_bc_widget_id` VARCHAR(255) DEFAULT NULL, `second_bc_custom_css` TEXT DEFAULT NULL, PRIMARY KEY (`id`) )" . $charset_collate . ";"; //check if table exists if ($wpdb->get_var("show tables like '" . $settings_table . "'") != $settings_table) { dbDelta($sql_table_settings); } } } } global $taboolaWP; $taboolaWP = new TaboolaWP();