external-permalinks-redux.php 7.74 KB
Newer Older
1 2 3
<?php
/*
Plugin Name: External Permalinks Redux
4
Plugin URI: http://www.thinkoomph.com/plugins-modules/external-permalinks-redux/
5
Description: Allows users to point WordPress objects (posts, pages, custom post types) to a URL of your choosing. Inspired by and backwards-compatible with <a href="http://txfx.net/wordpress-plugins/page-links-to/">Page Links To</a> by Mark Jaquith. Written for use on WordPress.com VIP.
6
Version: 1.0.4
7
Author: Erick Hitter & Oomph, Inc.
8
Author URI: http://www.thinkoomph.com/
Erick Hitter's avatar
Erick Hitter committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 24 25
*/

class external_permalinks_redux {
26
	/**
27 28
	 * Class variables
	 */
29 30
	protected static $instance;

31 32
	var $meta_key_target = '_links_to';
	var $meta_key_type = '_links_to_type';
33
	var $status_codes;
34

35 36 37 38 39
	/**
	 * Instantiate class as a singleton
	 *
	 * @return object
	 */
40 41 42
	static function get_instance() {
		if ( ! isset( self::$instance ) )
			self::$instance = new external_permalinks_redux;
43

44 45 46
		return self::$instance;
	}

47
	/**
48
	 * Register actions and filters
49
	 *
Erick Hitter's avatar
Erick Hitter committed
50 51
	 * @uses add_action
	 * @uses add_filter
52 53 54
	 * @return null
	 */
	function __construct() {
55
		add_action( 'init', array( $this, 'action_init' ), 0 ); // other init actions may rely on permalinks so filter early
56
		add_action( 'add_meta_boxes', array( $this, 'action_add_meta_boxes' ) );
57
		add_action( 'save_post', array( $this, 'action_save_post' ) );
58

59 60 61 62
		add_filter( 'post_link', array( $this, 'filter_post_permalink' ), 1, 2 );
		add_filter( 'post_type_link', array( $this, 'filter_post_permalink' ), 1, 2 );
		add_filter( 'page_link', array( $this, 'filter_page_link' ), 1, 2 );
		add_action( 'wp', array( $this, 'action_wp' ) );
63 64 65 66
	}

	/**
	 * Register plugin keys and status codes
67 68 69 70
	 *
	 * @uses apply_filters
	 * @action init
	 * @return null
71 72
	 */
	function action_init() {
73 74
		$this->meta_key_target = apply_filters( 'epr_meta_key_target', $this->meta_key_target );
		$this->meta_key_type = apply_filters( 'epr_meta_key_type', $this->meta_key_type );
75

76
		$status_codes = array(
77 78 79
			302 => __( 'Temporary (302)', 'external-permalinks-redux' ),
			301 => __( 'Permanent (301)', 'external-permalinks-redux' ),
		);
80
		$this->status_codes = apply_filters( 'epr_status_codes', $status_codes );
81
	}
82

83
	/**
84
	 * Add meta box
85
	 *
Erick Hitter's avatar
Erick Hitter committed
86
	 * @uses apply_filters
87
	 * @uses __
Erick Hitter's avatar
Erick Hitter committed
88
	 * @uses add_meta_box
89
	 * @action add_meta_boxes
90 91
	 * @return null
	 */
92
	function action_add_meta_boxes() {
93
		$post_types = apply_filters( 'epr_post_types', array( 'post', 'page' ) );
94

Erick Hitter's avatar
Erick Hitter committed
95
		if ( ! is_array( $post_types ) )
96
			return;
97

Erick Hitter's avatar
Erick Hitter committed
98
		foreach( $post_types as $post_type ) {
99 100 101 102 103 104 105 106
			$title  = apply_filters( 'epr_metabox_title', '', $post_type );

			if ( ! $title )
				$title = __( 'External Permalinks Redux', 'external-permalinks-redux' );

			add_meta_box( 'external-permalinks-redux', $title, array( $this, 'meta_box' ), $post_type, 'normal' );

			unset( $title );
Erick Hitter's avatar
Erick Hitter committed
107
		}
108
	}
109 110


111
	/**
112
	 * Render meta box
113
	 *
114
	 * @param object $post
Erick Hitter's avatar
Erick Hitter committed
115 116 117 118 119
	 * @uses _e
	 * @uses esc_url
	 * @uses get_post_meta
	 * @uses selected
	 * @uses wp_create_nonce
120 121 122 123 124
	 * @return string
	 */
	function meta_box( $post ) {
		$type = get_post_meta( $post->ID, $this->meta_key_type, true );
	?>
125
		<p class="epr-destination">
126
			<label for="epr-url"><?php _e( 'Destination Address:', 'external-permalinks-redux' ); ?></label><br />
127 128
			<input name="<?php echo $this->meta_key_target; ?>_url" class="large-text code" id="epr-url" type="text" value="<?php echo esc_url( get_post_meta( $post->ID, $this->meta_key_target, true ) ); ?>" />
		</p>
129 130 131

		<p class="description"><?php _e( 'To restore the original permalink, remove the link entered above.', 'external-permalinks-redux' ); ?></p>

132
		<p class="epr-separator">&nbsp;</p>
133

134
		<p class="epr-redirect-type">
135
			<label for="epr-type"><?php _e( 'Redirect Type:', 'external-permalinks-redux' ); ?></label>
136
			<select name="<?php echo $this->meta_key_target; ?>_type" id="epr-type">
137 138 139 140 141 142
				<option value=""><?php _e( '-- Select --', 'external-permalinks-redux' ); ?></option>
				<?php foreach ( $this->status_codes as $status_code => $explanation ) {
					echo '<option value="' . esc_attr( $status_code ) . '"';
					selected( $status_code, intval( $type ) );
					echo '>' . esc_attr( $explanation ) . '</option>';
				} ?>
143 144
			</select>
		</p>
145

146 147 148
		<input type="hidden" name="<?php echo $this->meta_key_target; ?>_nonce" value="<?php echo wp_create_nonce( 'external-permalinks-redux' ); ?>" />
	<?php
	}
149

150
	/**
151
	 * Save meta box input
152
	 *
153
	 * @param int $post_id
Erick Hitter's avatar
Erick Hitter committed
154 155 156 157
	 * @uses wp_verify_nonce
	 * @uses esc_url_raw
	 * @uses update_post_meta
	 * @uses delete_post_meta
158 159 160 161
	 * @action save_post
	 * @return null
	 */
	function action_save_post( $post_id ) {
Erick Hitter's avatar
Erick Hitter committed
162
		if ( isset( $_POST[ $this->meta_key_target . '_nonce' ] ) && wp_verify_nonce( $_POST[ $this->meta_key_target . '_nonce' ], 'external-permalinks-redux' ) ) {
163 164
			//Target
			$url = esc_url_raw( $_POST[ $this->meta_key_target . '_url' ] );
165

Erick Hitter's avatar
Erick Hitter committed
166
			if ( ! empty( $url ) )
167 168 169
				update_post_meta( $post_id, $this->meta_key_target, $url );
			else
				delete_post_meta( $post_id, $this->meta_key_target, $url );
170

171 172
			//Redirect type
			$type = intval( $_POST[ $this->meta_key_target . '_type' ] );
173

Erick Hitter's avatar
Erick Hitter committed
174
			if ( ! empty( $url ) && array_key_exists( $type, $this->status_codes ) )
175 176 177 178 179
				update_post_meta( $post_id, $this->meta_key_type, $type );
			else
				delete_post_meta( $post_id, $this->meta_key_type );
		}
	}
180

181
	/**
182
	 * Filter post and custom post type permalinks
183
	 *
184 185 186
	 * @param string $permalink
	 * @param object $post
	 * @uses get_post_meta
Erick Hitter's avatar
Erick Hitter committed
187 188
	 * @filter post_link
	 * @uses post_type_link
189 190 191
	 * @return string
	 */
	function filter_post_permalink( $permalink, $post ) {
Erick Hitter's avatar
Erick Hitter committed
192
		if ( $external_link = get_post_meta( $post->ID, $this->meta_key_target, true ) )
193
			$permalink = $external_link;
194

195 196
		return $permalink;
	}
197

198
	/**
199
	 * Filter page permalinks
200
	 *
201 202 203 204 205 206 207
	 * @param string $link
	 * @param int $id
	 * @uses get_post_meta
	 * @filter page_link
	 * @return string
	 */
	function filter_page_link( $link, $id ) {
Erick Hitter's avatar
Erick Hitter committed
208
		if ( $external_link = get_post_meta( $id, $this->meta_key_target, true ) )
209
			$link = $external_link;
210 211

		return $link;
212
	}
213

214
	/**
215
	 * Redirect to external link if object requested directly.
216
	 *
217 218
	 * @global $post
	 * @uses is_singular
Erick Hitter's avatar
Erick Hitter committed
219
	 * @uses get_post_meta
220
	 * @uses apply_filters
Erick Hitter's avatar
Erick Hitter committed
221
	 * @uses wp_redirect
222 223 224 225 226
	 * @action pre_get_posts
	 * @return null
	 */
	function action_wp() {
		global $post;
227

Erick Hitter's avatar
Erick Hitter committed
228
		if ( is_singular() && ( $link = get_post_meta( $post->ID, $this->meta_key_target, true ) ) ) {
229
			$type = intval( get_post_meta( $post->ID, $this->meta_key_type, true ) );
230 231
			$type = apply_filters( 'epr_status_code', $type, $link, $post );

Erick Hitter's avatar
Erick Hitter committed
232
			if ( ! $type )
233
				$type = 302;
234

235 236 237 238
			wp_redirect( $link, $type );
			exit;
		}
	}
239 240 241 242 243 244 245 246 247 248 249 250

	/**
	 ** BACKWARDS COMPATIBILITY
	 ** FUNCTIONS FOUNDS BELOW ARE DEPRECATED
	 **/

	/**
	 * Action changed and function renamed in v1.1.
	 */
	public function action_admin_init() {
		$this->action_add_meta_boxes();
	}
251
}
252 253 254

// Initialize the plugin if it hasn't already
external_permalinks_redux::get_instance();
255

256
/**
257 258
 * Wrapper for meta box function
 * Can be used as an alternative to the epr_post_types filter found in the plugin classes's action_admin_init function.
259
 *
260 261 262 263 264
 * @param object $post
 * @uses $external_permalinks_redux
 * @return string
 */
function external_permalinks_redux_meta_box( $post ) {
265
	external_permalinks_redux::get_instance()->meta_box( $post );
266 267
}
?>