. */ global $tfl_version; // current version global $tfl_upgradeversion; // least version to upgrade $tfl_upgradeversion = 42; $tfl_version = 44; class TwitterFriendlyLinks { var $settings = array(); var $defaultsettings = array(); function TwitterFriendlyLinks() { global $tfl_version; $this->defaultsettings = array( "version" => $tfl_version, "style" => "", // default style is example.com/123 "format" => "generic", // default format is generic (numbers only) "redirect" => 302, // temporary redirect by default "posts_enabled" => true, // posts enabled by default "pages_enabled" => false, // pages disabled by default "attachments_enabled" => false, // attachments disabled by default "twitter_tools_fix" => false, // disabled by deafult "askapache_google_404" => false, "tweet_this_fix" => false, "sociable_fix" => false, "ga_tracking" => "", "html_shortlink_rel" => false, "http_shortlink_rel" => false, "rel_canonical" => false, "tfl_core_notice" => 0, ); // Setup the settings by using the default as a base and then adding in any changed values // This allows settings arrays from old versions to be used even though they are missing values $usersettings = (array) get_option("twitter_friendly_links"); if (!isset($usersettings["version"])) $usersettings["version"] = 0; $this->settings = $this->defaultsettings; if ( $usersettings !== $this->defaultsettings ) { foreach ( (array) $usersettings as $key1 => $value1 ) { if ( is_array($value1) ) { foreach ( $value1 as $key2 => $value2 ) { $this->settings[$key1][$key2] = $value2; } } else { $this->settings[$key1] = $value1; } } } // Register general hooks add_action('template_redirect', array(&$this, 'template_redirect'), 9); add_action("admin_menu", array(&$this, "admin_menu")); add_action("admin_menu", array(&$this, "admin_menu_box")); // Filters and Actions if ($this->settings["twitter_tools_fix"]) add_filter("tweet_blog_post_url", "permalink_to_twitter_link", 10, 1); if ($this->settings["tweet_this_fix"]) add_filter("the_content", "tfl_tweet_this_fix", 10); if ($this->settings["sociable_fix"]) add_filter("sociable_link", "tfl_sociable_fix"); if ($this->settings["html_shortlink_rel"] || $this->settings["rel_canonical"]) add_action("wp_head", array(&$this, "wp_head")); // Notify the administrator if permalinks are switched off $permalink_structure = (isset($_POST['permalink_structure'])) ? $_POST['permalink_structure'] : get_option("permalink_structure"); if ($permalink_structure == "") add_action("admin_notices", array(&$this, "admin_notices")); } function wp_head() { // Mainly for relations if (($this->settings["posts_enabled"] && is_single()) || ($this->settings["pages_enabled"] && is_page()) || ($this->settings["attachments_enabled"] && is_attachment())) { global $post; $post_id = $post->ID; if ($this->settings["html_shortlink_rel"]) { $short_url = twitter_link($post_id); echo "\n"; } if ($this->settings["rel_canonical"]) { $permalink = get_permalink($post_id); echo "\n"; } } } function template_redirect() { $style = $this->settings["style"]; $format = $this->settings["format"]; $redirect = $this->settings["redirect"]; $ga_tracking = (strlen($this->settings["ga_tracking"]) > 1) ? "?".$this->settings["ga_tracking"] : ""; $uri = $_SERVER["REQUEST_URI"]; $home = get_option("home"); $home = str_replace("http://".$_SERVER["SERVER_NAME"], "", $home); $uri = strtolower(str_replace($home, "", $uri)); if (ereg("^/{$style}([0-9a-z]+)/?$", $uri, $regs)) { // Fix for the AskApache Google 404 plugin $this->settings["askapache_google_404"] = ($this->settings["askapache_google_404"] == "checked") ? true : false; if ($this->settings["askapache_google_404"]) { global $AskApacheGoogle404; remove_action("template_redirect", array($AskApacheGoogle404, 'template_redirect')); } $post_id = $regs[1]; if ($format == "base32") $post_id = tfl_base32($post_id, true); if (is_numeric($post_id)) { $posts = new WP_Query("p=$post_id&post_type=any"); if ($posts->have_posts()) { $posts->the_post(); $post = $posts->post; if (!$this->settings["posts_enabled"] && $post->post_type == "post") return; if (!$this->settings["pages_enabled"] && $post->post_type == "page") return; if (!$this->settings["attachments_enabled"] && $post->post_type == "attachment") return; global $wp_query; $wp_query->is_404 = false; wp_redirect(get_permalink().$ga_tracking, $redirect); } } } global $post; // Link relations if ($this->settings["http_shortlink_rel"]) if (($this->settings["posts_enabled"] && is_single()) || ($this->settings["pages_enabled"] && is_page()) || ($this->settings["attachments_enabled"] && is_attachment())) header("Link: <" . twitter_link($post_id) . ">; rel=shortlink"); } function admin_scripts() { $count_posts = wp_count_posts(); $total_posts = $count_posts->publish; $count_pages = wp_count_posts('page'); $total_pages = $count_pages->publish; $count_attachments = wp_count_posts('attachment'); $total_attachments = $count_attachments->inherit; $plugin_url = trailingslashit(get_bloginfo('wpurl')).PLUGINDIR.'/'. dirname(plugin_basename(__FILE__)); wp_enqueue_script('twitterfriendly_navsettings', $plugin_url.'/js/nav_settings.js', array('jquery')); wp_localize_script('twitterfriendly_navsettings', 'TwitterFriendly', array( "plugin_url" => $plugin_url, "total_posts" => $total_posts, "total_pages" => $total_pages, "total_attachments" => $total_attachments, )); } function admin_menu() { $twitter_friendly_admin = add_options_page('Twitter Friendly Links', 'Twitter Friendly Links', 8, __FILE__, array(&$this, 'options')); add_action("admin_print_scripts-$twitter_friendly_admin", array(&$this, "admin_scripts")); } function options() { $plugin_dir = "../".PLUGINDIR.'/'. dirname( plugin_basename(__FILE__) ); require_once($plugin_dir . "/options.php"); } function admin_menu_box() { if (function_exists("add_meta_box")) { add_meta_box("twitter_friendly_id", "Twitter Stuff", array(&$this, "admin_menu_inner_box"), "post", "side"); if ($this->settings["pages_enabled"]) add_meta_box("twitter_friendly_id", "Twitter Stuff", array(&$this, "admin_menu_inner_box"), "page", "side"); } else { add_action("dbx_post_advanced", array(&$this, "admin_menu_old_box")); if ($this->settings["pages_enabled"]) add_action("dbx_page_advanced", array(&$this, "admin_menu_old_box")); } } function admin_menu_old_box() { echo '
Please publish to get Twitter links.
"; return; } $post_id = $post->ID; $friendly_link = twitter_link($post_id); $title_length = utf8_strlen($post->post_title); $link_length = utf8_strlen($friendly_link); $overall_length = $title_length + $link_length; $excess = 140 - $overall_length; if ($excess < 0) { $excess *= -1; $title = mb_substr($post->post_title,0,$title_length-($excess+4),'UTF-8') . "... "; } else { $title = $post->post_title . " "; } echo "Friendly link: {$friendly_link}
"; echo "Tweet: {$title}{$friendly_link}
"; echo ""; } function admin_notices() { echo "You have to change your permalink structure for Twitter Friendly Links to work (don't use default).
Hey, this version of Twitter Friendly Links had some core changes. Make sure you double check your settings, which may have not been saved from your previous version, okay? Sorry for any inconvenience caused. Hide this message.