-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexternal-permalinks-redux.php
391 lines (331 loc) · 10.3 KB
/
external-permalinks-redux.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/**
* 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.3.2
* Author: Erick Hitter & Oomph, Inc.
* Author URI: http://www.thinkoomph.com/
* Text Domain: external-permalinks-redux
* Domain Path: /languages/
*
* 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
*/
// Include block-editor class.
require_once dirname( __FILE__ ) . '/inc/class-external-permalinks-redux-block-editor.php';
/**
* Class external_permalinks_redux.
*/
// phpcs:ignore PEAR.NamingConventions.ValidClassName, Squiz.Commenting.ClassComment.Missing
class external_permalinks_redux {
/**
* Singleton!
*
* @var self
*/
protected static $instance;
/**
* 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';
/**
* Supported redirect codes.
*
* @var array
*/
public $status_codes;
/**
* Supported post types.
*
* Cannot be used before `admin_init` hook.
*
* @var array
*/
private $post_types;
/**
* Instantiate class as a singleton.
*
* @return object
*/
public static function get_instance() {
if ( empty( self::$instance ) ) {
self::$instance = new self();
self::$instance->_setup();
}
return self::$instance;
}
/**
* Unused constructor.
*/
final private function __construct() {}
/**
* Allow access to certain private properties.
*
* @param string $name Property name.
* @return array|null
*/
public function __get( $name ) {
if ( 'post_types' === $name ) {
if ( ! did_action( 'admin_init' ) ) {
_doing_it_wrong(
__METHOD__,
'Cannot be used before `admin_init` hook.',
'1.0'
);
}
return $this->post_types;
}
return null;
}
/**
* Disallow setting private properties except via filters.
*
* @param string $name Property name.
* @param mixed $value Property value.
* @return false
*/
public function __set( $name, $value ) {
return false;
}
/**
* Check if certain private properties are set.
*
* @param string $name Property name.
* @return bool
*/
public function __isset( $name ) {
if ( 'post_types' === $name ) {
if ( ! did_action( 'admin_init' ) ) {
return false;
}
return is_array( $this->post_types )
&& ! empty( $this->post_types );
}
return false;
}
/**
* Register actions and filters.
*/
protected function _setup() {
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
add_action( 'init', array( $this, 'action_init' ), 0 ); // Other init actions may rely on permalinks so filter early.
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
add_action( 'save_post', array( $this, 'action_save_post' ) );
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' ) );
}
/**
* Load plugin translations.
*
* @return void
*/
public function load_textdomain() {
load_plugin_textdomain(
'external-permalinks-redux',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
);
}
/**
* Register plugin keys and status codes.
*
* @action init
*/
public function action_init() {
$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 );
$status_codes = array(
302 => __( 'Temporary (302)', 'external-permalinks-redux' ),
301 => __( 'Permanent (301)', 'external-permalinks-redux' ),
);
$this->status_codes = apply_filters( 'epr_status_codes', $status_codes );
}
/**
* Add meta box.
*/
public function action_admin_init() {
$this->post_types = apply_filters( 'epr_post_types', array( 'post', 'page' ) );
if ( ! is_array( $this->post_types ) ) {
return;
}
$this->post_types = array_flip( $this->post_types );
foreach ( array_keys( $this->post_types ) as $post_type ) {
$title = apply_filters( 'epr_metabox_title', '', $post_type );
if ( empty( $title ) ) {
$title = __( 'External Permalinks Redux', 'external-permalinks-redux' );
}
$this->post_types[ $post_type ] = $title;
if (
function_exists( 'use_block_editor_for_post_type' )
&& use_block_editor_for_post_type( $post_type )
) {
continue;
}
add_meta_box( 'external-permalinks-redux', $title, array( $this, 'meta_box' ), $post_type, 'normal' );
unset( $title );
}
}
/**
* Render meta box.
*
* @param object $post Post object.
*/
public function meta_box( $post ) {
$type = get_post_meta( $post->ID, $this->meta_key_type, true );
?>
<p class="epr-destination">
<label for="epr-url"><?php esc_html_e( 'Destination Address:', 'external-permalinks-redux' ); ?></label><br />
<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 ) ); ?>" />
</p>
<p class="description"><?php esc_html_e( 'To restore the original permalink, remove the link entered above.', 'external-permalinks-redux' ); ?></p>
<p class="epr-separator"> </p>
<p class="epr-redirect-type">
<label for="epr-type"><?php esc_html_e( 'Redirect Type:', 'external-permalinks-redux' ); ?></label>
<select name="<?php echo esc_attr( $this->meta_key_target ); ?>_type" id="epr-type">
<option value=""><?php esc_html_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, (int) $type );
echo '>' . esc_html( $explanation ) . '</option>';
}
?>
</select>
</p>
<?php
wp_nonce_field( 'external-permalinks-redux', $this->meta_key_target . '_nonce', false );
}
/**
* Save meta box input.
*
* @param int $post_id Post ID.
*/
public function action_save_post( $post_id ) {
if (
function_exists( 'use_block_editor_for_post' )
&& use_block_editor_for_post( $post_id )
) {
return;
}
if ( isset( $_POST[ $this->meta_key_target . '_nonce' ] ) && wp_verify_nonce( sanitize_text_field( $_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' ] ) : '';
if ( ! empty( $url ) ) {
update_post_meta( $post_id, $this->meta_key_target, $url );
} else {
delete_post_meta( $post_id, $this->meta_key_target, $url );
}
// Redirect type.
$type = isset( $_POST[ $this->meta_key_target . '_type' ] ) ? (int) $_POST[ $this->meta_key_target . '_type' ] : '';
if ( ! empty( $url ) && array_key_exists( $type, $this->status_codes ) ) {
update_post_meta( $post_id, $this->meta_key_type, $type );
} else {
delete_post_meta( $post_id, $this->meta_key_type );
}
}
}
/**
* Filter post and custom post type permalinks.
*
* @param string $permalink Post permalinks.
* @param object $post Post object.
* @return string
*/
public function filter_post_permalink( $permalink, $post ) {
$external_link = get_post_meta( $post->ID, $this->meta_key_target, true );
if ( ! empty( $external_link ) ) {
$permalink = $external_link;
}
return $permalink;
}
/**
* Filter page permalinks.
*
* @param string $link Page permalink.
* @param int $id Post ID.
* @return string
*/
public function filter_page_link( $link, $id ) {
$external_link = get_post_meta( $id, $this->meta_key_target, true );
if ( ! empty( $external_link ) ) {
$link = $external_link;
}
return $link;
}
/**
* Redirect to external link if object requested directly.
*/
public function action_wp() {
global $post;
if ( ! is_singular() ) {
return;
}
$redirect = $this->get_redirect_data( $post->ID );
if ( false === $redirect ) {
return;
}
// Unreasonable to validate redirect destination.
// phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
wp_redirect( $redirect['link'], $redirect['type'] );
exit;
}
/**
* Retrieve redirect data for a given post ID.
*
* @param int $post_id Post ID.
* @return array|bool
*/
public function get_redirect_data( $post_id ) {
if ( ! is_numeric( $post_id ) ) {
return false;
}
$link = get_post_meta( $post_id, $this->meta_key_target, true );
if ( empty( $link ) ) {
return false;
}
$type = (int) get_post_meta( $post_id, $this->meta_key_type, true );
$type = apply_filters( 'epr_status_code', $type, $link, get_post( $post_id ) );
if ( ! $type ) {
$type = 302;
}
return compact( 'link', 'type' );
}
}
// Initialize the plugin if it hasn't already.
external_permalinks_redux::get_instance();
External_Permalinks_Redux_Block_Editor::get_instance();
/**
* 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.
*
* @param object $post Post object.
*/
function external_permalinks_redux_meta_box( $post ) {
external_permalinks_redux::get_instance()->meta_box( $post );
}