external-permalinks-redux.php 7.81 KB
Newer Older
1
<?php
Erick Hitter's avatar
Erick Hitter committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/**
 * Plugin Name: External Permalinks Redux
 * Plugin URI: http://www.thinkoomph.com/plugins-modules/external-permalinks-redux/
 * 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.
 * Version: 1.1
 * Author: Erick Hitter & Oomph, Inc.
 * Author URI: http://www.thinkoomph.com/
 *
 * 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
 *
 * @package External_Permalinks_Redux
 */
26

Erick Hitter's avatar
Erick Hitter committed
27 28 29 30
/**
 * Class external_permalinks_redux.
 */
// phpcs:ignore PEAR.NamingConventions.ValidClassName, Squiz.Commenting.ClassComment.Missing
31
class external_permalinks_redux {
32
	/**
Erick Hitter's avatar
Erick Hitter committed
33 34 35
	 * Singleton!
	 *
	 * @var self
36
	 */
37 38
	protected static $instance;

Erick Hitter's avatar
Erick Hitter committed
39 40 41 42 43 44 45 46 47 48 49 50 51
	/**
	 * Redirect URL meta key.
	 *
	 * @var string
	 */
	public $meta_key_target = '_links_to';

	/**
	 * Redirect type meta key.
	 *
	 * @var string
	 */
	public $meta_key_type = '_links_to_type';
52

53
	/**
Erick Hitter's avatar
Erick Hitter committed
54 55 56 57 58 59 60 61
	 * Supported redirect codes.
	 *
	 * @var array
	 */
	public $status_codes;

	/**
	 * Instantiate class as a singleton.
62 63 64
	 *
	 * @return object
	 */
Erick Hitter's avatar
Erick Hitter committed
65
	public static function get_instance() {
Erick Hitter's avatar
Erick Hitter committed
66 67 68
		if ( ! isset( self::$instance ) ) {
			self::$instance = new self();
		}
69

70 71 72
		return self::$instance;
	}

73
	/**
Erick Hitter's avatar
Erick Hitter committed
74
	 * Register actions and filters.
75
	 */
Erick Hitter's avatar
Erick Hitter committed
76
	private function __construct() {
Erick Hitter's avatar
Erick Hitter committed
77
		add_action( 'init', array( $this, 'action_init' ), 0 ); // Other init actions may rely on permalinks so filter early.
78
		add_action( 'admin_init', array( $this, 'action_admin_init' ) );
79
		add_action( 'save_post', array( $this, 'action_save_post' ) );
80

81 82 83 84
		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' ) );
85 86 87
	}

	/**
Erick Hitter's avatar
Erick Hitter committed
88
	 * Register plugin keys and status codes.
89 90
	 *
	 * @action init
91
	 */
Erick Hitter's avatar
Erick Hitter committed
92
	public function action_init() {
93
		$this->meta_key_target = apply_filters( 'epr_meta_key_target', $this->meta_key_target );
Erick Hitter's avatar
Erick Hitter committed
94
		$this->meta_key_type   = apply_filters( 'epr_meta_key_type', $this->meta_key_type );
95

Erick Hitter's avatar
Erick Hitter committed
96
		$status_codes       = array(
97 98 99
			302 => __( 'Temporary (302)', 'external-permalinks-redux' ),
			301 => __( 'Permanent (301)', 'external-permalinks-redux' ),
		);
100
		$this->status_codes = apply_filters( 'epr_status_codes', $status_codes );
101
	}
102

103
	/**
Erick Hitter's avatar
Erick Hitter committed
104
	 * Add meta box.
105
	 */
Erick Hitter's avatar
Erick Hitter committed
106
	public function action_admin_init() {
107
		$post_types = apply_filters( 'epr_post_types', array( 'post', 'page' ) );
108

Erick Hitter's avatar
Erick Hitter committed
109
		if ( ! is_array( $post_types ) ) {
110
			return;
Erick Hitter's avatar
Erick Hitter committed
111
		}
112

Erick Hitter's avatar
Erick Hitter committed
113 114
		foreach ( $post_types as $post_type ) {
			$title = apply_filters( 'epr_metabox_title', '', $post_type );
115

Erick Hitter's avatar
Erick Hitter committed
116
			if ( ! $title ) {
117
				$title = __( 'External Permalinks Redux', 'external-permalinks-redux' );
Erick Hitter's avatar
Erick Hitter committed
118
			}
119 120 121 122

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

			unset( $title );
Erick Hitter's avatar
Erick Hitter committed
123
		}
124
	}
125 126


127
	/**
Erick Hitter's avatar
Erick Hitter committed
128
	 * Render meta box.
129
	 *
Erick Hitter's avatar
Erick Hitter committed
130
	 * @param object $post Post object.
131
	 */
Erick Hitter's avatar
Erick Hitter committed
132
	public function meta_box( $post ) {
133
		$type = get_post_meta( $post->ID, $this->meta_key_type, true );
Erick Hitter's avatar
Erick Hitter committed
134
		?>
135
		<p class="epr-destination">
136
			<label for="epr-url"><?php _e( 'Destination Address:', 'external-permalinks-redux' ); ?></label><br />
137
			<input name="<?php echo esc_attr( $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 ) ); ?>" />
138
		</p>
139 140 141

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

142
		<p class="epr-separator">&nbsp;</p>
143

144
		<p class="epr-redirect-type">
145
			<label for="epr-type"><?php _e( 'Redirect Type:', 'external-permalinks-redux' ); ?></label>
146
			<select name="<?php echo esc_attr( $this->meta_key_target ); ?>_type" id="epr-type">
147
				<option value=""><?php _e( '-- Select --', 'external-permalinks-redux' ); ?></option>
Erick Hitter's avatar
Erick Hitter committed
148
				<?php
Erick Hitter's avatar
Erick Hitter committed
149
				foreach ( $this->status_codes as $status_code => $explanation ) {
150
					echo '<option value="' . esc_attr( $status_code ) . '"';
Erick Hitter's avatar
Erick Hitter committed
151
					echo selected( $status_code, intval( $type ) );
152
					echo '>' . esc_attr( $explanation ) . '</option>';
Erick Hitter's avatar
Erick Hitter committed
153
				}
Erick Hitter's avatar
Erick Hitter committed
154
				?>
155 156
			</select>
		</p>
157

Erick Hitter's avatar
Erick Hitter committed
158
		<?php
Erick Hitter's avatar
Erick Hitter committed
159 160
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo wp_nonce_field( 'external-permalinks-redux', $this->meta_key_target . '_nonce', false );
161
	}
162

163
	/**
Erick Hitter's avatar
Erick Hitter committed
164
	 * Save meta box input.
165
	 *
Erick Hitter's avatar
Erick Hitter committed
166
	 * @param int $post_id Post ID.
167
	 */
Erick Hitter's avatar
Erick Hitter committed
168 169 170 171
	public function action_save_post( $post_id ) {
		if ( isset( $_POST[ $this->meta_key_target . '_nonce' ] ) && wp_verify_nonce( sanitize_text_field( stripslashes_deep( $_POST[ $this->meta_key_target . '_nonce' ] ) ), 'external-permalinks-redux' ) ) {
			// Target.
			$url = isset( $_POST[ $this->meta_key_target . '_url' ] ) ? esc_url_raw( $_POST[ $this->meta_key_target . '_url' ] ) : '';
172

Erick Hitter's avatar
Erick Hitter committed
173
			if ( ! empty( $url ) ) {
174
				update_post_meta( $post_id, $this->meta_key_target, $url );
Erick Hitter's avatar
Erick Hitter committed
175
			} else {
176
				delete_post_meta( $post_id, $this->meta_key_target, $url );
Erick Hitter's avatar
Erick Hitter committed
177
			}
178

Erick Hitter's avatar
Erick Hitter committed
179 180
			// Redirect type.
			$type = isset( $_POST[ $this->meta_key_target . '_type' ] ) ? (int) $_POST[ $this->meta_key_target . '_type' ] : '';
181

Erick Hitter's avatar
Erick Hitter committed
182
			if ( ! empty( $url ) && array_key_exists( $type, $this->status_codes ) ) {
183
				update_post_meta( $post_id, $this->meta_key_type, $type );
Erick Hitter's avatar
Erick Hitter committed
184
			} else {
185
				delete_post_meta( $post_id, $this->meta_key_type );
Erick Hitter's avatar
Erick Hitter committed
186
			}
187 188
		}
	}
189

190
	/**
Erick Hitter's avatar
Erick Hitter committed
191
	 * Filter post and custom post type permalinks.
192
	 *
Erick Hitter's avatar
Erick Hitter committed
193 194
	 * @param string $permalink Post permalinks.
	 * @param object $post Post object.
195 196
	 * @return string
	 */
Erick Hitter's avatar
Erick Hitter committed
197 198 199 200
	public function filter_post_permalink( $permalink, $post ) {
		$external_link = get_post_meta( $post->ID, $this->meta_key_target, true );

		if ( ! empty( $external_link ) ) {
201
			$permalink = $external_link;
Erick Hitter's avatar
Erick Hitter committed
202
		}
203

204 205
		return $permalink;
	}
206

207
	/**
Erick Hitter's avatar
Erick Hitter committed
208
	 * Filter page permalinks.
209
	 *
Erick Hitter's avatar
Erick Hitter committed
210 211
	 * @param string $link Page permalink.
	 * @param int    $id Post ID.
212 213
	 * @return string
	 */
Erick Hitter's avatar
Erick Hitter committed
214 215 216 217
	public function filter_page_link( $link, $id ) {
		$external_link = get_post_meta( $id, $this->meta_key_target, true );

		if ( ! empty( $external_link ) ) {
218
			$link = $external_link;
Erick Hitter's avatar
Erick Hitter committed
219
		}
220 221

		return $link;
222
	}
223

224
	/**
225 226
	 * Redirect to external link if object requested directly.
	 */
Erick Hitter's avatar
Erick Hitter committed
227
	public function action_wp() {
228
		global $post;
229

Erick Hitter's avatar
Erick Hitter committed
230 231 232 233 234 235 236 237
		if ( ! is_singular() ) {
			return;
		}

		$link = get_post_meta( $post->ID, $this->meta_key_target, true );

		if ( ! empty( $link ) ) {
			$type = (int) get_post_meta( $post->ID, $this->meta_key_type, true );
238 239
			$type = apply_filters( 'epr_status_code', $type, $link, $post );

Erick Hitter's avatar
Erick Hitter committed
240
			if ( ! $type ) {
241
				$type = 302;
Erick Hitter's avatar
Erick Hitter committed
242
			}
243

Erick Hitter's avatar
Erick Hitter committed
244 245
			// Unreasonable to validate redirect destination.
			// phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
246 247 248 249 250
			wp_redirect( $link, $type );
			exit;
		}
	}
}
251

Erick Hitter's avatar
Erick Hitter committed
252
// Initialize the plugin if it hasn't already.
253
external_permalinks_redux::get_instance();
254

255
/**
Erick Hitter's avatar
Erick Hitter committed
256 257 258 259
 * Wrapper for meta box function.
 *
 * Can be used as an alternative to the `epr_post_types` filter
 * found in the plugin class's `action_admin_init` function.
260
 *
Erick Hitter's avatar
Erick Hitter committed
261
 * @param object $post Post object.
262 263
 */
function external_permalinks_redux_meta_box( $post ) {
264
	external_permalinks_redux::get_instance()->meta_box( $post );
265
}