PathScope.php
2.1 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
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
<?php
namespace AC\Settings\Column;
use AC\Settings;
use AC\View;
class PathScope extends Settings\Column
implements Settings\FormatValue {
/**
* @var string
*/
private $path_scope;
protected function define_options() {
return [
'path_scope' => 'full',
];
}
public function create_view() {
$select = $this->create_element( 'select', 'path_scope' )
->set_options( [
'full' => __( 'Full Path', 'codepress-admin-columns' ),
'relative-domain' => __( 'Relative to domain', 'codepress-admin-columns' ),
'relative-uploads' => __( 'Relative to main uploads folder', 'codepress-admin-columns' ),
'local' => __( 'Local Path', 'codepress-admin-columns' ),
] );
$view = new View( [
'label' => __( 'Path scope', 'codepress-admin-columns' ),
'tooltip' => __( 'Part of the file path to display', 'codepress-admin-columns' ),
'setting' => $select,
] );
return $view;
}
/**
* @return string
*/
public function get_path_scope() {
return $this->path_scope;
}
/**
* @param string $path_scope
*
* @return bool
*/
public function set_path_scope( $path_scope ) {
$this->path_scope = $path_scope;
return true;
}
public function format( $value, $original_value ) {
$file = $value;
$value = '';
if ( $file ) {
switch ( $this->get_path_scope() ) {
case 'relative-domain' :
$file = str_replace( 'https://', 'http://', $file );
$url = str_replace( 'https://', 'http://', home_url( '/' ) );
if ( strpos( $file, $url ) === 0 ) {
$file = '/' . substr( $file, strlen( $url ) );
}
break;
case 'relative-uploads' :
$file = str_replace( 'https://', 'http://', $file );
$upload_dir = wp_upload_dir();
$url = str_replace( 'https://', 'http://', $upload_dir['baseurl'] );
if ( strpos( $file, $url ) === 0 ) {
$file = substr( $file, strlen( $url ) );
}
break;
case 'local' :
$file = get_attached_file( $original_value );
break;
}
$value = $file;
}
return $value;
}
}