Page Links To - Version 2.4

Version Description

  • Rewrote using Singleton best practices
  • Fixed a regex bug that could break current menu highlighting. props skarab
Download this release

Release Info

Developer markjaquith
Plugin Icon wp plugin Page Links To
Version 2.4
Comparing to
See all releases

Code changes from version 2.3 to 2.4

Files changed (2) hide show
  1. page-links-to.php +202 -137
  2. readme.txt +6 -2
page-links-to.php CHANGED
@@ -1,14 +1,14 @@
1
<?php
2
/*
3
Plugin Name: Page Links To
4
- Plugin URI: http://txfx.net/code/wordpress/page-links-to/
5
Description: Allows you to point WordPress pages or posts to a URL of your choosing. Good for setting up navigational links to non-WP sections of your site or to off-site resources.
6
- Version: 2.3
7
Author: Mark Jaquith
8
Author URI: http://coveredwebservices.com/
9
*/
10
11
- /* Copyright 2005-2008 Mark Jaquith (email: mark.gpl@txfx.net)
12
13
This program is free software; you can redistribute it and/or modify
14
it under the terms of the GNU General Public License as published by
@@ -36,174 +36,239 @@ if ( !function_exists( 'esc_attr' ) ) {
36
}
37
}
38
39
- function txfx_get_post_meta_by_key( $key ) {
40
- global $wpdb;
41
- return $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $key ) );
42
- }
43
-
44
- function txfx_get_page_links_to_meta () {
45
- global $wpdb, $page_links_to_cache, $blog_id;
46
47
- if ( !isset( $page_links_to_cache[$blog_id] ) )
48
- $links_to = txfx_get_post_meta_by_key( '_links_to' );
49
- else
50
- return $page_links_to_cache[$blog_id];
51
52
- if ( !$links_to ) {
53
- $page_links_to_cache[$blog_id] = false;
54
- return false;
55
}
56
57
- foreach ( (array) $links_to as $link )
58
- $page_links_to_cache[$blog_id][$link->post_id] = $link->meta_value;
59
60
- return $page_links_to_cache[$blog_id];
61
- }
62
63
- function txfx_get_page_links_to_targets () {
64
- global $wpdb, $page_links_to_target_cache, $blog_id;
65
66
- if ( !isset( $page_links_to_target_cache[$blog_id] ) )
67
- $links_to = txfx_get_post_meta_by_key( '_links_to_target' );
68
- else
69
- return $page_links_to_target_cache[$blog_id];
70
71
- if ( !$links_to ) {
72
- $page_links_to_target_cache[$blog_id] = false;
73
- return false;
74
}
75
76
- foreach ( (array) $links_to as $link )
77
- $page_links_to_target_cache[$blog_id][$link->post_id] = $link->meta_value;
78
79
- return $page_links_to_target_cache[$blog_id];
80
- }
81
82
- function txfx_plt_add_meta_box( $page, $context ) {
83
- if ( ( 'page' === $page || 'post' === $page ) && 'advanced' === $context )
84
- add_meta_box( 'page-links-to', 'Page Links To', 'txfx_plt_meta_box', $page, 'advanced', 'low' );
85
- }
86
87
- function txfx_plt_meta_box() {
88
- global $post;
89
- echo '<p>';
90
- wp_nonce_field( 'txfx_plt', '_txfx_pl2_nonce', false, true );
91
- echo '</p>';
92
- $url = get_post_meta( $post->ID, '_links_to', true);
93
- if ( !$url )
94
- $url = 'http://';
95
- ?>
96
- <p>Point to this URL: <input name="txfx_links_to" type="text" style="width:75%" id="txfx_links_to" value="<?php echo attribute_escape( $url ); ?>" /></p>
97
- <p><label for="txfx_links_to_new_window"><input type="checkbox" name="txfx_links_to_new_window" id="txfx_links_to_new_window" value="_blank" <?php checked( '_blank', get_post_meta( $post->ID, '_links_to_target', true ) ); ?>> Open this link in a new window</label></p>
98
- <p><label for="txfx_links_to_302"><input type="checkbox" name="txfx_links_to_302" id="txfx_links_to_302" value="302" <?php checked( '302', get_post_meta( $post->ID, '_links_to_type', true ) ); ?>> Use a temporary <code>302</code> redirect (default is a permanent <code>301</code> redirect)</label></p>
99
- <?php
100
- }
101
102
- function txfx_plt_save_meta_box( $post_ID ) {
103
- if ( wp_verify_nonce( $_REQUEST['_txfx_pl2_nonce'], 'txfx_plt' ) ) {
104
- if ( isset( $_POST['txfx_links_to'] ) && strlen( $_POST['txfx_links_to'] ) > 0 && $_POST['txfx_links_to'] !== 'http://' ) {
105
- $link = stripslashes( $_POST['txfx_links_to'] );
106
- if ( 0 === strpos( $link, 'www.' ) )
107
- $link = 'http://' . $link; // Starts with www., so add http://
108
- update_post_meta( $post_ID, '_links_to', $link );
109
- if ( isset( $_POST['txfx_links_to_new_window'] ) )
110
- update_post_meta( $post_ID, '_links_to_target', '_blank' );
111
- else
112
delete_post_meta( $post_ID, '_links_to_target' );
113
- if ( isset( $_POST['txfx_links_to_302'] ) )
114
- update_post_meta( $post_ID, '_links_to_type', '302' );
115
- else
116
delete_post_meta( $post_ID, '_links_to_type' );
117
- } else {
118
- delete_post_meta( $post_ID, '_links_to' );
119
- delete_post_meta( $post_ID, '_links_to_target' );
120
- delete_post_meta( $post_ID, '_links_to_type' );
121
}
122
}
123
- return $post_ID;
124
- }
125
-
126
-
127
- function txfx_filter_links_to_pages ($link, $post) {
128
- $page_links_to_cache = txfx_get_page_links_to_meta();
129
130
- // Really strange, but page_link gives us an ID and post_link gives us a post object
131
- $id = ( $post->ID ) ? $post->ID : $post;
132
133
- if ( $page_links_to_cache[$id] )
134
- $link = esc_url( $page_links_to_cache[$id] );
135
-
136
- return $link;
137
- }
138
139
- function txfx_redirect_links_to_pages() {
140
- if ( !is_single() && !is_page() )
141
- return;
142
143
- global $wp_query;
144
-
145
- $link = get_post_meta( $wp_query->post->ID, '_links_to', true );
146
147
- if ( !$link )
148
- return;
149
150
- $redirect_type = get_post_meta( $wp_query->post->ID, '_links_to_type', true );
151
- $redirect_type = ( $redirect_type = '302' ) ? '302' : '301';
152
- wp_redirect( $link, $redirect_type );
153
- exit;
154
- }
155
156
- function txfx_page_links_to_highlight_tabs( $pages ) {
157
- $page_links_to_cache = txfx_get_page_links_to_meta();
158
- $page_links_to_target_cache = txfx_get_page_links_to_targets();
159
160
- if ( !$page_links_to_cache && !$page_links_to_target_cache )
161
- return $pages;
162
163
- $this_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
164
- $targets = array();
165
166
- foreach ( (array) $page_links_to_cache as $id => $page ) {
167
- if ( isset( $page_links_to_target_cache[$id] ) )
168
- $targets[$page] = $page_links_to_target_cache[$id];
169
170
- if ( str_replace( 'http://www.', 'http://', $this_url ) == str_replace( 'http://www.', 'http://', $page ) || ( is_home() && str_replace( 'http://www.', 'http://', trailingslashit( get_bloginfo( 'home' ) ) ) == str_replace( 'http://www.', 'http://', trailingslashit( $page ) ) ) ) {
171
- $highlight = true;
172
- $current_page = esc_url( $page );
173
}
174
- }
175
176
- if ( count( $targets ) ) {
177
- foreach ( $targets as $p => $t ) {
178
- $p = esc_url( $p );
179
- $t = esc_attr( $t );
180
- $pages = str_replace( '<a href="' . $p . '" ', '<a href="' . $p . '" target="' . $t . '" ', $pages );
181
}
182
- }
183
184
- if ( $highlight ) {
185
- $pages = preg_replace( '| class="([^"]+)current_page_item"|', ' class="$1"', $pages ); // Kill default highlighting
186
- $pages = preg_replace( '|<li class="([^"]+)"><a href="' . $current_page . '"|', '<li class="$1 current_page_item"><a href="' . $current_page . '"', $pages );
187
}
188
189
- return $pages;
190
- }
191
-
192
- function txfx_plt_init() {
193
- if ( get_option( 'txfx_plt_schema_version' ) < 3 ) {
194
- global $wpdb;
195
- $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to' WHERE meta_key = 'links_to' " );
196
- $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to_target' WHERE meta_key = 'links_to_target' " );
197
- $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to_type' WHERE meta_key = 'links_to_type' " );
198
- wp_cache_flush();
199
- update_option( 'txfx_plt_schema_version', 3 );
200
- }
201
}
202
203
- add_filter( 'wp_list_pages', 'txfx_page_links_to_highlight_tabs', 9 );
204
- add_action( 'template_redirect', 'txfx_redirect_links_to_pages' );
205
- add_filter( 'page_link', 'txfx_filter_links_to_pages', 20, 2 );
206
- add_filter( 'post_link', 'txfx_filter_links_to_pages', 20, 2 );
207
- add_action( 'do_meta_boxes', 'txfx_plt_add_meta_box', 10, 2 );
208
- add_action( 'save_post', 'txfx_plt_save_meta_box' );
209
- add_action( 'init', 'txfx_plt_init' );
1
<?php
2
/*
3
Plugin Name: Page Links To
4
+ Plugin URI: http://txfx.net/wordpress-plugins/page-links-to/
5
Description: Allows you to point WordPress pages or posts to a URL of your choosing. Good for setting up navigational links to non-WP sections of your site or to off-site resources.
6
+ Version: 2.4
7
Author: Mark Jaquith
8
Author URI: http://coveredwebservices.com/
9
*/
10
11
+ /* Copyright 2005-2010 Mark Jaquith (email: mark.gpl@txfx.net)
12
13
This program is free software; you can redistribute it and/or modify
14
it under the terms of the GNU General Public License as published by
36
}
37
}
38
39
+ class CWS_PageLinksTo {
40
+ var $targets;
41
+ var $links;
42
43
+ /**
44
+ * PHP 4 constructor
45
+ */
46
+ function CWS_PageLinksTo() {
47
+ return $this->__construct();
48
+ }
49
50
+ /**
51
+ * PHP 5 constructor
52
+ */
53
+ function __construct() {
54
+ add_action( 'init', array( &$this, 'init' ) );
55
}
56
57
+ /**
58
+ * Bootstraps the upgrade process and registers all the hooks.
59
+ */
60
+ function init() {
61
+ $this->maybe_upgrade();
62
+ add_filter( 'wp_list_pages', array( &$this, 'wp_list_pages' ) );
63
+ add_action( 'template_redirect', array( &$this, 'template_redirect' ) );
64
+ add_filter( 'page_link', array( &$this, 'link' ), 20, 2 );
65
+ add_filter( 'post_link', array( &$this, 'link' ), 20, 2 );
66
+ add_action( 'do_meta_boxes', array( &$this, 'do_meta_boxes' ), 20, 2 );
67
+ add_action( 'save_post', array( &$this, 'save_post' ) );
68
+ }
69
70
+ /**
71
+ * Performs an upgrade for older versions. Hides the keys so they only show in the plugin's UI
72
+ */
73
+ function maybe_upgrade() {
74
+ if ( get_option( 'txfx_plt_schema_version' ) < 3 ) {
75
+ global $wpdb;
76
+ $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to' WHERE meta_key = 'links_to' " );
77
+ $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to_target' WHERE meta_key = 'links_to_target' " );
78
+ $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to_type' WHERE meta_key = 'links_to_type' " );
79
+ wp_cache_flush();
80
+ update_option( 'txfx_plt_schema_version', 3 );
81
+ }
82
+ }
83
+ /**
84
+ * Returns post ids and meta values that have a given key
85
+ * @param string $key post meta key
86
+ * @return array an array of objects with post_id and meta_value properties
87
+ */
88
+ function meta_by_key( $key ) {
89
+ global $wpdb;
90
+ return $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $key ) );
91
+ }
92
93
+ /**
94
+ * Returns all links for the current site
95
+ * @return array an array of links, keyed by post ID
96
+ */
97
+ function get_links() {
98
+ global $wpdb, $blog_id;
99
+
100
+ if ( !isset( $this->links[$blog_id] ) )
101
+ $links_to = $this->meta_by_key( '_links_to' );
102
+ else
103
+ return $this->links[$blog_id];
104
+
105
+ if ( !$links_to ) {
106
+ $this->links[$blog_id] = false;
107
+ return false;
108
+ }
109
110
+ foreach ( (array) $links_to as $link )
111
+ $this->links[$blog_id][$link->post_id] = $link->meta_value;
112
113
+ return $this->links[$blog_id];
114
}
115
116
+ /**
117
+ * Returns all targets for the current site
118
+ * @return array an array of targets, keyed by post ID
119
+ */
120
+ function get_targets () {
121
+ global $wpdb, $page_links_to_target_cache, $blog_id;
122
+
123
+ if ( !isset( $this->targets[$blog_id] ) )
124
+ $links_to = $this->meta_by_key( '_links_to_target' );
125
+ else
126
+ return $this->targets[$blog_id];
127
+
128
+ if ( !$links_to ) {
129
+ $this->targets[$blog_id] = false;
130
+ return false;
131
+ }
132
133
+ foreach ( (array) $links_to as $link )
134
+ $this->targets[$blog_id][$link->post_id] = $link->meta_value;
135
136
+ return $this->targets[$blog_id];
137
+ }
138
139
+ /**
140
+ * Adds the meta box to the post or page edit screen
141
+ * @param string $page the name of the current page
142
+ * @param string $context the current context
143
+ */
144
+ function do_meta_boxes( $page, $context ) {
145
+ if ( ( 'page' === $page || 'post' === $page ) && 'advanced' === $context )
146
+ add_meta_box( 'page-links-to', 'Page Links To', array( &$this, 'meta_box' ), $page, 'advanced', 'low' );
147
+ }
148
+
149
+ function meta_box() {
150
+ global $post;
151
+ echo '<p>';
152
+ wp_nonce_field( 'txfx_plt', '_txfx_pl2_nonce', false, true );
153
+ echo '</p>';
154
+ $url = get_post_meta( $post->ID, '_links_to', true);
155
+ if ( !$url )
156
+ $url = 'http://';
157
+ ?>
158
+ <p>Point to this URL: <input name="txfx_links_to" type="text" style="width:75%" id="txfx_links_to" value="<?php echo esc_attr( $url ); ?>" /></p>
159
+ <p><label for="txfx_links_to_new_window"><input type="checkbox" name="txfx_links_to_new_window" id="txfx_links_to_new_window" value="_blank" <?php checked( '_blank', get_post_meta( $post->ID, '_links_to_target', true ) ); ?>> Open this link in a new window</label></p>
160
+ <p><label for="txfx_links_to_302"><input type="checkbox" name="txfx_links_to_302" id="txfx_links_to_302" value="302" <?php checked( '302', get_post_meta( $post->ID, '_links_to_type', true ) ); ?>> Use a temporary <code>302</code> redirect (default is a permanent <code>301</code> redirect)</label></p>
161
+ <?php
162
+ }
163
164
+ /**
165
+ * Saves data on post save
166
+ * @param int $post_ID a post ID
167
+ * @return int the post ID that was passed in
168
+ */
169
+ function save_post( $post_ID ) {
170
+ if ( wp_verify_nonce( $_REQUEST['_txfx_pl2_nonce'], 'txfx_plt' ) ) {
171
+ if ( isset( $_POST['txfx_links_to'] ) && strlen( $_POST['txfx_links_to'] ) > 0 && $_POST['txfx_links_to'] !== 'http://' ) {
172
+ $link = stripslashes( $_POST['txfx_links_to'] );
173
+ if ( 0 === strpos( $link, 'www.' ) )
174
+ $link = 'http://' . $link; // Starts with www., so add http://
175
+ update_post_meta( $post_ID, '_links_to', $link );
176
+ if ( isset( $_POST['txfx_links_to_new_window'] ) )
177
+ update_post_meta( $post_ID, '_links_to_target', '_blank' );
178
+ else
179
+ delete_post_meta( $post_ID, '_links_to_target' );
180
+ if ( isset( $_POST['txfx_links_to_302'] ) )
181
+ update_post_meta( $post_ID, '_links_to_type', '302' );
182
+ else
183
+ delete_post_meta( $post_ID, '_links_to_type' );
184
+ } else {
185
+ delete_post_meta( $post_ID, '_links_to' );
186
delete_post_meta( $post_ID, '_links_to_target' );
187
delete_post_meta( $post_ID, '_links_to_type' );
188
+ }
189
}
190
+ return $post_ID;
191
}
192
193
+ /**
194
+ * Filter for post or page links
195
+ * @param string $link the URL for the post or page
196
+ * @param int|object $post Either a post ID or a post object
197
+ * @return string output URL
198
+ */
199
+ function link( $link, $post ) {
200
+ $links = $this->get_links();
201
202
+ // Really strange, but page_link gives us an ID and post_link gives us a post object
203
+ $id = ( is_object( $post ) && $post->ID ) ? $post->ID : $post;
204
205
+ if ( $links[$id] )
206
+ $link = esc_url( $links[$id] );
207
208
+ return $link;
209
+ }
210
211
+ /**
212
+ * Performs a redirect, if appropriate
213
+ */
214
+ function template_redirect() {
215
+ if ( !is_single() && !is_page() )
216
+ return;
217
218
+ global $wp_query;
219
220
+ $link = get_post_meta( $wp_query->post->ID, '_links_to', true );
221
222
+ if ( !$link )
223
+ return;
224
225
+ $redirect_type = get_post_meta( $wp_query->post->ID, '_links_to_type', true );
226
+ $redirect_type = ( $redirect_type = '302' ) ? '302' : '301';
227
+ wp_redirect( $link, $redirect_type );
228
+ exit;
229
+ }
230
231
+ /**
232
+ * Filters the list of pages to alter the links and targets
233
+ * @param string $pages the wp_list_pages() HTML block from WordPress
234
+ * @return string the modified HTML block
235
+ */
236
+ function wp_list_pages( $pages ) {
237
+ $links = $this->get_links();
238
+ $page_links_to_target_cache = $this->get_targets();
239
+
240
+ if ( !$links && !$page_links_to_target_cache )
241
+ return $pages;
242
+
243
+ $this_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
244
+ $targets = array();
245
+
246
+ foreach ( (array) $links as $id => $page ) {
247
+ if ( isset( $page_links_to_target_cache[$id] ) )
248
+ $targets[$page] = $page_links_to_target_cache[$id];
249
+
250
+ if ( str_replace( 'http://www.', 'http://', $this_url ) == str_replace( 'http://www.', 'http://', $page ) || ( is_home() && str_replace( 'http://www.', 'http://', trailingslashit( get_bloginfo( 'home' ) ) ) == str_replace( 'http://www.', 'http://', trailingslashit( $page ) ) ) ) {
251
+ $highlight = true;
252
+ $current_page = esc_url( $page );
253
+ }
254
+ }
255
256
+ if ( count( $targets ) ) {
257
+ foreach ( $targets as $p => $t ) {
258
+ $p = esc_url( $p );
259
+ $t = esc_attr( $t );
260
+ $pages = str_replace( '<a href="' . $p . '" ', '<a href="' . $p . '" target="' . $t . '" ', $pages );
261
+ }
262
}
263
264
+ if ( $highlight ) {
265
+ $pages = preg_replace( '| class="([^"]+)current_page_item"|', ' class="$1"', $pages ); // Kill default highlighting
266
+ $pages = preg_replace( '|<li class="([^"]+)"><a href="' . preg_quote( $current_page ) . '"|', '<li class="$1 current_page_item"><a href="' . $current_page . '"', $pages );
267
}
268
269
+ return $pages;
270
}
271
272
}
273
274
+ new CWS_PageLinksTo;
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
=== Plugin Name ===
2
Contributors: markjaquith
3
- Donate link: http://txfx.net/code/wordpress/
4
Tags: page, redirect, link, external link, repoint
5
Requires at least: 2.7
6
- Tested up to: 2.9.1
7
Stable tag: trunk
8
9
Lets you make a WordPress page or post link to a URL of your choosing, instead of its WordPress post or page.
@@ -56,6 +56,10 @@ Yes. Linking to `/my-photos.php` is a good idea, as it'll still work if you move
56
57
== Changelog ==
58
59
= 2.3 =
60
* Fixed a bug with current menu item highlighting
61
1
=== Plugin Name ===
2
Contributors: markjaquith
3
+ Donate link: http://txfx.net/wordpress-plugins/donate
4
Tags: page, redirect, link, external link, repoint
5
Requires at least: 2.7
6
+ Tested up to: 3.0.1
7
Stable tag: trunk
8
9
Lets you make a WordPress page or post link to a URL of your choosing, instead of its WordPress post or page.
56
57
== Changelog ==
58
59
+ = 2.4 =
60
+ * Rewrote using Singleton best practices
61
+ * Fixed a regex bug that could break current menu highlighting. props skarab
62
+
63
= 2.3 =
64
* Fixed a bug with current menu item highlighting
65