improve engagement and increase revenue. To get started: 1) Click the "Activate" link to the left of this description, 2) Sign up to get your Adknowledge Engage™ API key, and 3) Go to your Adknowledge Engage configuration to save your API key and set up this plugin. * Author: Adknowledge * Author URI: http://www.adknowledge.com * Version: 1.1 * Text Domain: related content */ define('ADENG_PLUGIN_NAME', plugin_basename(__FILE__)); define('ADENG_DOMAIN', 'app.engage.bidsystem.com'); define('ADENG_CHECK_DOMAIN', 'api.engage.bidsystem.com'); define('ADENG_SIDEBAR_URL', 'http://api.engage.bidsystem.com/engage_plugin_sidebar.html'); /** * Method that is available to the users to insert into their themes. * @usage */ function adk_engage() { global $post; $apiKey = get_option('adeng_api_key'); $aId = get_option('adeng_affiliate_id'); $verified = get_option('adeng_api_key_verified'); $display = (array)get_option('adeng_display'); $inject = false; $script = ''; if (! $apiKey) { // no API key has been set, do nothing return; } $script = '
'; echo $script; } function adeng_display($content) { global $post; $apiKey = get_option('adeng_api_key'); $aId = get_option('adeng_affiliate_id'); $verified = get_option('adeng_api_key_verified'); $display = (array)get_option('adeng_display'); $excludes = (array)get_option('adeng_exclude'); $inject = false; $script = ''; if (! $apiKey) { // no API key has been set, simply return the content untouched return $content; } if (is_front_page() && in_array('front', $display)) { $inject = true; } elseif (is_home() && in_array('home', $display)) { $inject = true; } elseif (is_single() && in_array('single', $display)) { $inject = true; $cats = wp_get_post_categories($post->ID); if (isset($excludes['categories'])) { foreach ($cats as $id) { if (in_array($id, $excludes['categories'])) { $inject = false; break; } } } $tags = wp_get_post_tags($post->ID); if (isset($excludes['tags'])) { foreach ($tags as $tag) { if (in_array($tag->term_id, $excludes['tags'])) { $inject = false; break; } } } } elseif (is_page() && in_array('page', $display)) { $inject = true; $cats = wp_get_post_categories($post->ID); if (isset($excludes['categories'])) { foreach ($cats as $id) { if (in_array($id, $excludes['categories'])) { $inject = false; break; } } } $tags = wp_get_post_tags($post->ID); if (isset($excludes['tags'])) { foreach ($tags as $tag) { if (in_array($tag->term_id, $excludes['tags'])) { $inject = false; break; } } } } elseif (is_category() && in_array('category', $display)) { $inject = true; $cats = wp_get_post_categories($post->ID); if (isset($excludes['categories'])) { foreach ($cats as $id) { if (in_array($id, $excludes['categories'])) { $inject = false; break; } } } } elseif (is_tag() && in_array('tag', $display)) { $inject = true; $tags = wp_get_post_tags($post->ID); if (isset($excludes['tags'])) { foreach ($tags as $tag) { if (in_array($tag->term_id, $excludes['tags'])) { $inject = false; break; } } } } elseif (is_author() && in_array('author', $display)) { $inject = true; } elseif (is_date() && in_array('date', $display)) { $inject = true; } elseif (is_search() && in_array('search', $display)) { $inject = true; } elseif (is_attachment() && in_array('attachments', $display)) { $inject = true; } if ($inject) { $script = '
'; $top = (bool)get_option('adeng_display_top', 0); $bottom = (bool)get_option('adeng_display_bottom', 0); if ($top) { $content = $script . $content; } if ($bottom) { $content = $content . $script; } } return $content; } // add admin options page function adeng_add_submenu_page(){ add_menu_page(__('Settings','adeng'), __('Adknowledge Engage','adeng'), 'manage_options', 'adeng-main', 'adeng_basic_form'); add_submenu_page('adeng-main', __('Basic Settings','adeng'), __('Basic Settings','adeng'), 'manage_options', 'adeng-main', 'adeng_basic_form'); } // Add settings link on plugin page function adeng_settings_link($links) { $settings_link = 'Settings'; array_unshift($links, $settings_link); return $links; } function adeng_rewrite_rules($incoming) { global $wp_rewrite; $apiKey = get_option('adeng_api_key'); $adengRules = array(); if ($apiKey) { $adengRules = array( $apiKey.'.txt(/)?' => 'index.php?adeng_robots=1', ); } $wp_rewrite->rules = $adengRules + $wp_rewrite->rules; } function adeng_query_vars($vars) { $vars[] = 'adeng_robots'; return $vars; } function adeng_robots_page() { global $wp_query, $wpdb; if (isset($wp_query->query_vars['adeng_robots']) && $wp_query->query_vars['adeng_robots'] == 1) { $base = get_site_url(); // categories $cat = get_categories('number=1'); $txt = ''; $catTxt = ''; if (count($cat) > 0) { $cat = $cat[0]; $url = get_category_link($cat->term_id); $parts = explode('/', $url); unset($parts[count($parts)-1], $parts[count($parts)-1]); $url = implode('/', $parts) .'/'; $url = str_replace($base, '', $url); $catTxt = $url; } // users $tagTxt = '/tag/'; $userTxt = '/author/'; $archives= '/archives/'; // date-based archives $permaTxt = ''; // get the starting year $rows = $wpdb->get_results("SELECT DISTINCT YEAR( post_date ) AS year, MONTH (post_date) AS month FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' ORDER BY post_date DESC" ); $now = date("Y"); foreach ($rows as $row) { $permaTxt .= 'Disallow: /'. $row->year .'/'. $row->month .'/$'. "\n"; $permaTxt .= 'Disallow: /'. $row->year .'/'. $row->month .'/page/'. "\n"; } $txt = "Disallow: /wp-\nDisallow: /page/\nDisallow: $catTxt\nDisallow: $userTxt\nDisallow: $tagTxt\nDisallow: $archives\n$permaTxt"; header("Content-Type: text/plain"); die($txt); } } function adeng_basic_form() { global $wpdb; include dirname(__FILE__) .'/adeng_settings.php'; } function adeng_update_settings() { global $wpdb; $css = $_POST['css']; $display = (array)$_POST['display']; $top = isset($_POST['top']) ? 1 : 0; $bottom = isset($_POST['bottom']) ? 1 : 0; $show = array(); foreach ($display as $key => $val) { $show[] = $key; } // exlusion list $excluded = array( 'categories' => array(), 'tags' => array(), 'users' => array() ); if (isset($_POST['exclude']) && !empty($_POST['exclude'])) { // categories if (isset($_POST['exclude']['category']) && !empty($_POST['exclude']['category'])) { $excluded['categories'] = $_POST['exclude']['category']; } if (isset($_POST['exclude']['tags']) && !empty($_POST['exclude']['tags'])) { $excluded['tags'] = $_POST['exclude']['tags']; } if (isset($_POST['exclude']['users']) && !empty($_POST['exclude']['users'])) { $excluded['users'] = $_POST['exclude']['users']; } } update_option('adeng_exclude', $excluded); update_option('adeng_inline_css', $css); update_option('adeng_display', $show); update_option('adeng_display_top', $top); update_option('adeng_display_bottom', $bottom); header("Location: admin.php?page=adeng-main&saved=1"); exit; } function adeng_verify_api() { global $wp_rewrite; $old = get_option('adeng_api_key'); $apiKey = $_POST['api_key']; $affId = $_POST['affiliate_id']; // verify the keys $fp = @fsockopen(ADENG_CHECK_DOMAIN, 80, $errno, $errstr, 30); $xml = ''; $resp = ''; if (!$fp) { die(json_encode(array('ack' => 'ERR', 'msg' => $errstr))); } else { $out = "GET /?aid=$affId&apikey=$apiKey HTTP/1.1\r\n"; $out .= "Host: ". ADENG_CHECK_DOMAIN ."\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { $resp .= fgets($fp, 128); } fclose($fp); $parts = explode("\r\n\r\n", $resp); $xml = $parts[1]; $parser = xml_parser_create(); xml_parse_into_struct($parser, $xml, $tags); $valid = false; foreach ((array)$tags as $tag) { if ($tag['tag'] == 'RESULT' && $tag['value'] == 'true') { $valid = true; break; } } } if ($valid) { update_option('adeng_api_key_verified', true); update_option('adeng_api_key', $apiKey); update_option('adeng_affiliate_id', $affId); // flush rewrite rules $wp_rewrite->flush_rules(); die(json_encode(array('ack' => 'OK'))); } else { update_option('adeng_api_key_verified', false); update_option('adeng_api_key', $apiKey); update_option('adeng_affiliate_id', $affId); die(json_encode(array('ack' => 'ERR', 'msg' => 'Invalid API Key/Affiliate ID'))); } } function adeng_print_css() { $css = get_option('adeng_inline_css'); if (! $css || empty($css)) { return; } echo "\n".''."\n"; } function adeng_admin_scripts() { wp_enqueue_script('jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'); wp_enqueue_style('jquery-colorpicker', plugins_url() .'/adknowledge-engage/colorpicker/css/colorpicker.css'); wp_enqueue_script('jquery-colorpicker', plugins_url() .'/adknowledge-engage/colorpicker/js/colorpicker.js'); ?> 'Display related content') ); } function widget($args, $instance) { extract( $args ); echo $before_widget; adk_engage(); echo $after_widget; } function update($new, $old) { } function form($instance) { } } function adeng_register_widget() { register_widget('AdEngWidget'); } add_filter("plugin_action_links_". ADENG_PLUGIN_NAME, 'adeng_settings_link' ); add_filter("generate_rewrite_rules", "adeng_rewrite_rules"); add_filter("query_vars", "adeng_query_vars"); add_action('template_redirect', 'adeng_robots_page'); add_filter('the_content', 'adeng_display', -10000); add_filter('the_excerpt', 'adeng_display', -10000); add_action('wp_print_styles', 'adeng_print_css'); add_action('admin_enqueue_scripts', 'adeng_admin_scripts'); add_action('admin_menu', 'adeng_add_submenu_page'); add_action('admin_post_adeng_update_settings', 'adeng_update_settings'); add_action('admin_post_adeng_update_advanced', 'adeng_update_advanced'); add_action('wp_ajax_adeng_verify_api', 'adeng_verify_api'); add_action('widgets_init', 'adeng_register_widget'); register_activation_hook(__FILE__, 'adeng_activate');