prefix}referrer_detector"; $rdetector_db_version = '1.0'; $rdetector_text_domain = 'referrer_detector'; $rdetector_plugin_dir = get_settings('siteurl') . '/wp-content/plugins/referrer-detector/'; $rdetector_wpadmin_index_page = get_settings('siteurl') . '/wp-admin/index.php'; $rdetector_help_url = 'http://www.phoenixheart.net/2008/11/referrer_detector'; define('RDETECTOR_CUSTOM_TAG', '{referrer_detector}'); define('RDETECTOR_NONCE_ACTION', md5($rdetector_plugin_name . $rdetector_wpadmin_index_page)); load_plugin_textdomain($rdetector_text_domain); $rdetector_add_help = '

To add or edit an entry, you’re provided with a simple form with 5 fields.

'; $rdetector_generic_help = '

Those options are used to specify if you want to show the greeting box on every post, page, before or after… Notice that if you can include a {referrer_detector} anywhere in your post to show the welcome text. If the visitor didn’t come from any of the referrers, don’t worry, the plugin is smart enough to hide that ugly tag from your post.

Starting from version 2.0, a template tag <?php if (function_exists(\'referrer_detector\')) referrer_detector(); ?> can be placed somewhere in the theme files (most likely index.php) to display a greet box too.

'; $rdetector_exclude_help = '

Here you can specify a list of urls (comma-seperated). If the visitor is comming from one of these urls, the welcome box will not be show.
For example, by adding google.com/reader into the list, you make sure that the readers won\'t see the box, which is good since they should have registered to your feed already.

'; $rdetector_options = array( 'rdetector_add_to_every_post' => array( 'type' => 'select', 'label' => __('Automatically display welcome message on every post? *', $rdetector_text_domain), 'options' => array ('yes', 'no'), 'default_value' => 'yes', ), 'rdetector_add_to_every_page' => array( 'type' => 'select', 'label' => __('Automatically display welcome message on every page? *', $rdetector_text_domain), 'options' => array ('yes', 'no'), 'default_value' => 'no', ), 'rdetector_message_position' => array( 'type' => 'select', 'label' => __('The position of the welcome message (related to page/post content)', $rdetector_text_domain), 'options' => array ('before', 'after'), 'default_value' => 'before', ), 'rdetector_close_icon' => array( 'type' => 'select', 'label' => __('Display an icon to let user close the message?', $rdetector_text_domain), 'options' => array ('yes', 'no'), 'default_value' => 'yes', ), ); /** * @desc Creates a new table to hold the data */ function rdetector_install() { global $wpdb, $rdetector_table_name, $rdetector_db_version, $rdetector_plugin_dir; if ($wpdb->get_var("SHOW TABLES LIKE '$rdetector_table_name'") == $rdetector_table_name) { rdetector_generate_js_data(); return false; } $sql = "CREATE TABLE $rdetector_table_name ( `id` smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `url` varchar(255) NOT NULL, `icon` varchar(255) NOT NULL, `welcome_text` text NOT NULL, `enabled` tinyint(4) NOT NULL default '1', PRIMARY KEY (`id`), KEY `url` (`url`) );"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); rdetector_add_default_entries(); rdetector_generate_js_data(); add_option('referrer_detector_db_version', $rdetector_db_version); } # Hook into the activation process to create the table register_activation_hook(__FILE__, 'rdetector_install'); /** * @desc Adds some entries as default */ function rdetector_add_default_entries() { global $wpdb, $rdetector_table_name, $rdetector_plugin_dir; $rss_link = get_bloginfo('rss2_url'); $sql = "INSERT INTO `$rdetector_table_name` (`name`, `url`, `icon`, `welcome_text`, `enabled`) VALUES ('Google', 'google.com', '{$rdetector_plugin_dir}images/icons/google.png', 'Welcome Googler! If you find this page useful, why not subscribe to the RSS feed for more interesting posts in the future?', 1), ('Digg', 'digg.com', '{$rdetector_plugin_dir}images/icons/digg.png', 'Hello fellow Digger! If you find this story useful, please digg it!', 1), ('StumbleUpon', 'stumbleupon.com', '{$rdetector_plugin_dir}images/icons/stumbleupon.png', 'Hello fellow Stumbler! Don\'t forget to give me a thumbs up if you like this page!', 1), ('Technograti', 'technorati.com', '{$rdetector_plugin_dir}images/icons/technorati.png', 'Hello fellow Technorati user! Don\'t forget to favorite this blog if you like it!', 1), ('Twitter', 'twitter.com', '{$rdetector_plugin_dir}images/icons/twitter.png', 'Hello fellow Twitter user! Don\'t forget to Twit this post if you like it, or follow me on Twitter if you find me interesting.', 1), ('Del.icio.us', 'del.icio.us', '{$rdetector_plugin_dir}images/icons/delicious.png', 'Hello fellow Delicious user! Feel free to bookmark this page for future reference if you like it!', 1), ('Delicious.com', 'delicious.com', '{$rdetector_plugin_dir}images/icons/delicious.png', 'Hello fellow Delicious user! Feel free to bookmark this page for future reference if you like it!', 1), ('Yahoo', 'search.yahoo.com', '{$rdetector_plugin_dir}images/icons/yahoo.png', 'Welcome fellow Yahooligan! If you find this page useful, you might want to subscribe to the RSS feed for updates on this topic.', 1), ('Live', 'search.live.com', '{$rdetector_plugin_dir}images/icons/live.png', 'Welcome fellow Live Search user! If you find this page useful, you might want to subscribe to the RSS feed for updates on this topic.', 1), ('Lifehacker', 'lifehacker.com', '', 'Hello fellow Lifehacker reader! If you find this page useful, you might want to subscribe to the RSS feed for updates on this topic.', 1), ('TechCrunch', 'techcrunch.com', '{$rdetector_plugin_dir}images/icons/techcrunch.png', 'Hello fellow TechCrunch reader! If you find this page useful, you might want to subscribe to the RSS feed for updates on this topic.', 1), ('9rules', '9rules.com', '{$rdetector_plugin_dir}images/icons/9rules.png', 'Hello fellow 9rules reader! If you find this page useful, you might want to subscribe to the RSS feed for updates on this topic.', 1), ('Gizmodo', 'gizmodo.com', '', 'Hello fellow Gizmodo reader! If you find this page useful, you might want to subscribe to the RSS feed for updates on this topic.', 1), ('TechRadar', 'techradar.com', '', 'Hello fellow TechRadar reader! If you find this page useful, you might want to subscribe to the RSS feed for updates on this topic.', 1), ('Reddit', 'reddit.com', '{$rdetector_plugin_dir}images/icons/reddit.png', 'Hello fellow Reddit user! If you find this story useful, please vote it up!', 1);"; $results = $wpdb->query($sql); } /** * @desc Hooks into the admin page's header */ function rdetector_include_scripts() { global $rdetector_plugin_dir; echo( '' . '' . '' . '' . '' . '' ); } /** * @desc Creates the options form */ function rdetector_options_form() { rdetector_include_scripts(); global $rdetector_plugin_name, $rdetector_table_name, $wpdb, $rdetector_plugin_dir, $rdetector_text_domain, $rdetector_wpadmin_index_page, $rdetector_help_url, $rdetector_add_help, $rdetector_generic_help, $rdetector_exclude_help; printf('

%s

', wp_create_nonce(RDETECTOR_NONCE_ACTION), __('Referrer Detector Options', $rdetector_text_domain), __('Entry List', $rdetector_text_domain), __('Add Entry', $rdetector_text_domain), __('Edit Entry', $rdetector_text_domain), __('Excluded URLs', $rdetector_text_domain), __('Generic Options', $rdetector_text_domain)); // the current entries printf('
%s

%s

', rdetector_get_entry_table(), __('Warning: This will delete ALL of your custom entries (if any)! Are you sure?'), __('Restore default entries'), $rdetector_plugin_dir); // the edit div printf(' ', __('Click "View/Edit" on the entry list to edit.', $rdetector_text_domain), __('Get help with this', $rdetector_text_domain), __($rdetector_add_help, $rdetector_text_domain), $rdetector_plugin_dir); // the Add div printf('

%s

%s





(%s)

', __('Get help with this', $rdetector_text_domain), __($rdetector_add_help, $rdetector_text_domain), __('Name', $rdetector_text_domain), __('Url', $rdetector_text_domain), __('Welcome message', $rdetector_text_domain), __('Icon', $rdetector_text_domain), __('Optional', $rdetector_text_domain), __('This entry is active', $rdetector_text_domain), __('Add this!', $rdetector_text_domain), $rdetector_plugin_dir ); // the exluded urls printf('

%s

%s

', __('Get help with this', $rdetector_text_domain), __($rdetector_exclude_help, $rdetector_text_domain), get_option('rdetector_excluded_urls'), __('Exclude these out!', $rdetector_text_domain)); // the generic options div printf('

%s

%s
', __('Get help with this', $rdetector_text_domain), __($rdetector_generic_help, $rdetector_text_domain)); global $rdetector_options; foreach ($rdetector_options as $key => $config) { $value = get_option($key); if (!$value) $value = $config['default_value']; $select_options = ''; foreach ($config['options'] as $o) { $select_options .= sprintf(' ', $o, $o == $value ? 'selected="selected"' : '', $o); } printf('


', $key, $config['label'], $key, $key, $select_options); } printf('

%s

', __("(*) If set to No, you may use a custom tag to manually display the welcome message on your posts.
Alternatively, you may put a template tag in your template files (most likely index.php)."), __('Save these options', $rdetector_text_domain)); printf('
', $rdetector_plugin_dir); } function rdetector_get_entry_table() { global $rdetector_table_name, $wpdb, $rdetector_text_domain; $sql = "SELECT * FROM $rdetector_table_name ORDER BY `name`"; $entries = $wpdb->get_results($sql); $table = sprintf(' ', __('Name', $rdetector_text_domain), __('Url', $rdetector_text_domain), __('Icon', $rdetector_text_domain), __('Welcome Message', $rdetector_text_domain), __('Action', $rdetector_text_domain)); foreach ($entries as $entry) { $table .= rdetector_format_entry_html($entry->id, $entry->name, $entry->url, $entry->icon, $entry->welcome_text, $entry->enabled == 1); } $table .= '
Name Url Icon Welcome Message Action
'; return $table; } /** * @desc Adds the Options menu item */ function rdetector_menu_items() { global $rdetector_text_domain; add_options_page( __('Referrer Detector', $rdetector_text_domain), __('Referrer Detector', $rdetector_text_domain), 8, basename(__FILE__), 'rdetector_options_form' ); } # Hook into the settings menu add_action('admin_menu', 'rdetector_menu_items'); /** * @desc Handles the form POST data to save the options */ function rdetector_request_handler() { global $rdetector_text_domain; if (!isset($_POST['rdetector_action'])) return false; if (!wp_verify_nonce($_POST['nonce'], RDETECTOR_NONCE_ACTION)) { die(__('Security check failed', $rdetector_text_domain)); } switch($_POST['rdetector_action']) { case 'activate': case 'deactivate': rdetector_activate_entry($_POST['rdetector_action'] == 'activate'); break; case 'add': rdetector_add_entry(); break; case 'edit': rdetector_edit_entry(); break; case 'update': rdetector_edit_entry(true); break; case 'delete': redetector_delete_entry(); break; case 'restore': redetector_restore_default_entries(); break; case 'exclude': // for now treat the excluded data as generic case 'generic': redetector_save_generic_options(); break; default: return false; } exit(); } # Hook into the init action to handle the options request add_action('init', 'rdetector_request_handler', 5); function rdetector_add_entry() { // a little validation if (!$errors = rdetector_validate_post_data()) { rdetector_send_fake_error(); } global $wpdb, $rdetector_table_name; $message = rdetector_strip_tags($_POST['message']); $sql = $wpdb->prepare("INSERT INTO `$rdetector_table_name`(`name`, `url`, `icon`, `welcome_text`, `enabled`) VALUES(%s, %s, %s, %s, %d)", $_POST['name'], $_POST['url'], $_POST['icon'], $message, isset($_POST['enabled'])); $result = $wpdb->query($sql); rdetector_generate_js_data(); echo rdetector_format_entry_html($wpdb->insert_id, $_POST['name'], $_POST['url'], $_POST['icon'], $message, isset($_POST['enabled'])); } /** * @desc Validates the POST data */ function rdetector_validate_post_data($is_editing = false) { global $rdetector_text_domain; $errors = array(); if (!isset($_POST['name']) || !trim($_POST['name'])) { $errors[] = __('Entry name is required', $rdetector_text_domain); } if (!isset($_POST['url']) || !trim($_POST['url'])) { $errors[] = __('URL is required', $rdetector_text_domain); } if (!isset($_POST['message']) || !trim($_POST['message'])) { $errors[] = __('Welcome message is required', $rdetector_text_domain); } if ($is_editing && (!isset($_POST['id']) || !trim($_POST['id']))) { $errors[] = __('You must specify an ID', $rdetector_text_domain); } if (empty($errors)) return true; printf ('

%s

', __('Your input is not valid:', $rdetector_text_domain), implode('
  • ', $errors)); return false; } function rdetector_format_entry_html($id, $name, $url, $icon, $welcome_text, $enabled) { global $rdetector_plugin_dir, $rdetector_text_domain; return sprintf(' %s %s %s %s | %s | %s %s', $enabled ? __('active', $rdetector_text_domain) : __('inactive', $rdetector_text_domain), $id, stripslashes($name), $url, $icon ? $icon : "{$rdetector_plugin_dir}images/icons/default.png", stripslashes($welcome_text), $id, $enabled ? 'deactivate' : 'activate', $enabled ? __('Deactivate', $rdetector_text_domain) : __('Activate', $rdetector_text_domain), $id, __('Edit/View', $rdetector_text_domain), $id, __('Are you sure you want to delete this entry?', $rdetector_text_domain), __('Delete', $rdetector_text_domain), $id, $rdetector_plugin_dir, PHP_EOL); } /** * @desc (De)Activates an entry */ function rdetector_activate_entry($is_activating) { global $wpdb, $rdetector_table_name; $sql = sprintf('UPDATE `%s` SET `enabled`=%s WHERE `id`=%s', $rdetector_table_name, $is_activating ? 1 : 0, intval($_POST['id'])); $wpdb->query($sql); if (!mysql_affected_rows()) { rdetector_send_fake_error(); } rdetector_generate_js_data(); $is_activating ? _e('Deactivate', $rdetector_text_domain) : _e('Activate', $rdetector_text_domain); } /** * @desc Edits an entry */ function rdetector_edit_entry($is_updating = false) { global $wpdb, $rdetector_table_name; $id = intval($_POST['id']); if (!$is_updating) { $sql = "SELECT * FROM `$rdetector_table_name` WHERE `id`=$id"; $entry = $wpdb->get_row($sql); if (empty($entry)) { rdetector_send_fake_error(); } printf('





    (%s)

    ', __('Name', $rdetector_text_domain), addslashes($entry->name), __('Url', $rdetector_text_domain), $entry->url, __('Welcome message', $rdetector_text_domain), stripslashes(htmlentities($entry->welcome_text)), __('Icon', $rdetector_text_domain), $entry->icon, __('Optional', $rdetector_text_domain), $entry->enabled == 1 ? 'checked="checked"' : '', __('This entry is active', $rdetector_text_domain), $entry->id, __('Save it!', $rdetector_text_domain), __('Cancel', $rdetector_text_domain)); } else { if (!$errors = rdetector_validate_post_data(true)) { return false; } global $wpdb, $rdetector_table_name; $message = rdetector_strip_tags($_POST['message']); $sql = $wpdb->prepare("UPDATE `$rdetector_table_name` SET `name`=%s, `url`=%s, `icon`=%s, `welcome_text`=%s, `enabled`=%d WHERE `id`=%d", $_POST['name'], $_POST['url'], $_POST['icon'], $message, isset($_POST['enabled']), intval($_POST['id'])); $result = $wpdb->query($sql); rdetector_generate_js_data(); echo rdetector_format_entry_html(intval($_POST['id']), $_POST['name'], $_POST['url'], $_POST['icon'], $message, isset($_POST['enabled'])); } } function redetector_delete_entry() { if (!isset($_POST['id']) || trim($_POST['id']) == '') { rdetector_send_fake_error(); } global $wpdb, $rdetector_table_name; $sql = $wpdb->prepare("DELETE FROM `$rdetector_table_name` WHERE `id`=%d LIMIT 1", intval($_POST['id'])); $wpdb->query($sql); if (!mysql_affected_rows()) { rdetector_send_fake_error(); } rdetector_generate_js_data(); } /** * @desc Restore the default entries */ function redetector_restore_default_entries() { global $wpdb, $rdetector_table_name; $wpdb->query("TRUNCATE $rdetector_table_name"); rdetector_add_default_entries(); rdetector_generate_js_data(); echo rdetector_get_entry_table(); } /** * @desc Saves the generic options of the plugin */ function redetector_save_generic_options() { // we have 2 cases if (isset($_POST['rdetector_excluded_urls'])) { // 1. Excluded URLS $urls = explode(',', $_POST['rdetector_excluded_urls']); array_walk($urls, 'rdetector_trim_value'); update_option('rdetector_excluded_urls', implode(',', $urls)); } else { // 2. "Real" generic options global $rdetector_options, $rdetector_text_domain; foreach ($rdetector_options as $key => $config) { if (isset($_POST[$key])) { update_option($key, $_POST[$key]); } } } rdetector_generate_js_data(); _e("Settings saved.", $rdetector_text_domain); } function rdetector_trim_value(&$value) { $value = trim($value); } /** * @desc (Re)Generates the data in JS format */ function rdetector_generate_js_data() { // if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') return true; global $wpdb, $rdetector_table_name, $rdetector_options, $rdetector_plugin_dir, $rdetector_text_domain; $sql = "SELECT * FROM `$rdetector_table_name` WHERE `enabled`=1"; $entries = $wpdb->get_results($sql); // the entries $data[] = 'var rdetector_entries = new Array();'; foreach ($entries as $entry) { if (empty($entry->icon)) $entry->icon = "{$rdetector_plugin_dir}images/icons/default.png"; $key = base64_encode($entry->url); $data[] = sprintf('rdetector_entries["%s"] = new Array();', $key); $data[] = sprintf('rdetector_entries["%s"]["message"] = "%s";', $key, base64_encode(stripslashes($entry->welcome_text))); $data[] = sprintf('rdetector_entries["%s"]["icon"] = "%s";', $key, base64_encode($entry->icon)); } // the options $data[] = 'var rdetector_options = new Array();'; foreach ($rdetector_options as $key => $config) { $value = get_option($key); $data[] = sprintf('rdetector_options["%s"] = "%s";', $key, empty($value) ? $config['default_value'] : $value); } // don't forget the excluded URLs $data[] = sprintf('rdetector_options["rdetector_excluded_urls"] = "%s";', base64_encode(get_option('rdetector_excluded_urls'))); // save if (!$handle = fopen("../wp-content/plugins/referrer-detector/js/data.js", 'w')) { exit(__("Error: Could not open wp-content/plugins/referrer-detector/js/data.js for writing, please check file permission.", $rdetector_text_domain)); } fwrite($handle, implode(PHP_EOL, $data)); fclose($handle); } /** * @desc Strips unallowed tags from the welcome message */ function rdetector_strip_tags($message) { $allowable_tags = '