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