maybe_upgrade(); load_plugin_textdomain( 'page-links-to', false, basename( dirname( __FILE__ ) ) . '/languages' ); add_filter( 'wp_list_pages', array( $this, 'wp_list_pages' ) ); add_action( 'template_redirect', array( $this, 'template_redirect' ) ); add_filter( 'page_link', array( $this, 'link' ), 20, 2 ); add_filter( 'post_link', array( $this, 'link' ), 20, 2 ); add_filter( 'post_type_link', array( $this, 'link', ), 20, 2 ); add_action( 'do_meta_boxes', array( $this, 'do_meta_boxes' ), 20, 2 ); add_action( 'save_post', array( $this, 'save_post' ) ); add_filter( 'wp_nav_menu_objects', array( $this, 'wp_nav_menu_objects' ), 10, 2 ); add_action( 'load-post.php', array( $this, 'load_post' ) ); add_filter( 'the_posts', array( $this, 'the_posts' ) ); } /** * Performs an upgrade for older versions. Hides the keys so they only show in the plugin's UI */ function maybe_upgrade() { if ( get_option( 'txfx_plt_schema_version' ) < 3 ) { global $wpdb; $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to' WHERE meta_key = 'links_to' " ); $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to_target' WHERE meta_key = 'links_to_target' " ); $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to_type' WHERE meta_key = 'links_to_type' " ); wp_cache_flush(); update_option( 'txfx_plt_schema_version', 3 ); } } /** * Returns post ids and meta values that have a given key * @param string $key post meta key * @return array an array of objects with post_id and meta_value properties */ function meta_by_key( $key ) { global $wpdb; return $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $key ) ); } /** * Returns all links for the current site * @return array an array of links, keyed by post ID */ function get_links() { global $wpdb, $blog_id; if ( !isset( $this->links[$blog_id] ) ) $links_to = $this->meta_by_key( '_links_to' ); else return $this->links[$blog_id]; if ( !$links_to ) { $this->links[$blog_id] = false; return false; } foreach ( (array) $links_to as $link ) $this->links[$blog_id][$link->post_id] = $link->meta_value; return $this->links[$blog_id]; } /** * Returns all targets for the current site * @return array an array of targets, keyed by post ID */ function get_targets () { global $wpdb, $page_links_to_target_cache, $blog_id; if ( !isset( $this->targets[$blog_id] ) ) $links_to = $this->meta_by_key( '_links_to_target' ); else return $this->targets[$blog_id]; if ( !$links_to ) { $this->targets[$blog_id] = false; return false; } foreach ( (array) $links_to as $link ) $this->targets[$blog_id][$link->post_id] = $link->meta_value; return $this->targets[$blog_id]; } /** * Adds the meta box to the post or page edit screen * @param string $page the name of the current page * @param string $context the current context */ function do_meta_boxes( $page, $context ) { // Plugins that use custom post types can use this filter to hide the PLT UI in their post type. $plt_post_types = apply_filters( 'page-links-to-post-types', array_keys( get_post_types( array('show_ui' => true ) ) ) ); if ( in_array( $page, $plt_post_types ) && 'advanced' === $context ) add_meta_box( 'page-links-to', 'Page Links To', array( $this, 'meta_box' ), $page, 'advanced', 'low' ); } function meta_box() { global $post; echo '

'; wp_nonce_field( 'txfx_plt', '_txfx_pl2_nonce', false, true ); echo '

'; $url = get_post_meta( $post->ID, '_links_to', true); if ( !$url ) { $linked = false; $url = 'http://'; } else { $linked = true; } ?>

0 && $_POST['txfx_links_to'] !== 'http://' ) { $link = trim( stripslashes( $_POST['txfx_links_to'] ) ); if ( 0 === strpos( $link, 'www.' ) ) $link = 'http://' . $link; // Starts with www., so add http:// update_post_meta( $post_ID, '_links_to', $link ); if ( isset( $_POST['txfx_links_to_new_window'] ) ) update_post_meta( $post_ID, '_links_to_target', '_blank' ); else delete_post_meta( $post_ID, '_links_to_target' ); } else { delete_post_meta( $post_ID, '_links_to' ); delete_post_meta( $post_ID, '_links_to_target' ); delete_post_meta( $post_ID, '_links_to_type' ); } } return $post_ID; } /** * Filter for post or page links * @param string $link the URL for the post or page * @param int|object $post Either a post ID or a post object * @return string output URL */ function link( $link, $post ) { $links = $this->get_links(); // Really strange, but page_link gives us an ID and post_link gives us a post object $id = ( is_object( $post ) && $post->ID ) ? $post->ID : $post; if ( isset( $links[$id] ) && $links[$id] ) $link = esc_url( $links[$id] ); return $link; } /** * Performs a redirect, if appropriate */ function template_redirect() { if ( !is_single() && !is_page() ) return; global $wp_query; $link = get_post_meta( $wp_query->post->ID, '_links_to', true ); if ( !$link ) return; wp_redirect( $link, 301 ); exit; } /** * Filters the list of pages to alter the links and targets * @param string $pages the wp_list_pages() HTML block from WordPress * @return string the modified HTML block */ function wp_list_pages( $pages ) { $highlight = false; $links = $this->get_links(); $page_links_to_target_cache = $this->get_targets(); if ( !$links && !$page_links_to_target_cache ) return $pages; $this_url = ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $targets = array(); foreach ( (array) $links as $id => $page ) { if ( isset( $page_links_to_target_cache[$id] ) ) $targets[$page] = $page_links_to_target_cache[$id]; if ( str_replace( 'http://www.', 'http://', $this_url ) == str_replace( 'http://www.', 'http://', $page ) || ( is_home() && str_replace( 'http://www.', 'http://', trailingslashit( get_bloginfo( 'url' ) ) ) == str_replace( 'http://www.', 'http://', trailingslashit( $page ) ) ) ) { $highlight = true; $current_page = esc_url( $page ); } } if ( count( $targets ) ) { foreach ( $targets as $p => $t ) { $p = esc_url( $p ); $t = esc_attr( $t ); $pages = str_replace( 'get_targets(); $new_items = array(); foreach ( $items as $item ) { if ( isset( $page_links_to_target_cache[$item->object_id] ) ) $item->target = $page_links_to_target_cache[$item->object_id]; $new_items[] = $item; } return $new_items; } function load_post() { if ( isset( $_GET['post'] ) ) { if ( get_post_meta( absint( $_GET['post'] ), '_links_to', true ) ) { add_action( 'admin_notices', array( $this, 'notify_of_external_link' ) ); } } } function notify_of_external_link() { ?>

Note: This content is pointing to an alternate URL. Use the “Page Links To” box to change this behavior.', 'page-links-to' ); ?>

get_targets(); if ( is_array( $page_links_to_target_cache) && count( $page_links_to_target_cache ) ) { $pids = array(); foreach ( (array) $posts as $p ) $pids[$p->ID] = $p->ID; $targets = array_keys( array_intersect_key( $page_links_to_target_cache, $pids ) ); if ( count( $targets ) ) { array_walk( $targets, array( $this, 'id_to_url_callback' ) ); $targets = array_unique( $targets ); $this->targets_on_this_page = $targets; wp_enqueue_script( 'jquery' ); add_action( 'wp_head', array( $this, 'targets_in_new_window_via_js' ) ); } } return $posts; } function targets_in_new_window_via_js() { ?>