Media.php
1.14 KB
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
<?php
namespace AIOSEO\Plugin\Common\Main;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Media class.
*
* @since 4.0.0
*/
class Media {
/**
* Construct method.
*
* @since 4.0.0
*/
public function __construct() {
add_action( 'template_redirect', [ $this, 'attachmentRedirect' ], 1 );
}
/**
* If the user wants to redirect attachment pages, this is where we do it.
*
* @since 4.0.0
*
* @return void
*/
public function attachmentRedirect() {
if ( ! is_attachment() ) {
return;
}
if (
! aioseo()->dynamicOptions->searchAppearance->postTypes->has( 'attachment' )
) {
return;
}
$redirect = aioseo()->dynamicOptions->searchAppearance->postTypes->attachment->redirectAttachmentUrls;
if ( 'disabled' === $redirect ) {
return;
}
if ( 'attachment' === $redirect ) {
$url = wp_get_attachment_url( get_queried_object_id() );
if ( empty( $url ) ) {
return;
}
return wp_safe_redirect( $url, 301, AIOSEO_PLUGIN_SHORT_NAME );
}
global $post;
if ( ! empty( $post->post_parent ) ) {
wp_safe_redirect( urldecode( get_permalink( $post->post_parent ) ), 301 );
}
}
}