custompaths.php
4.93 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
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
<?php if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class NF_FU_Admin_Controllers_CustomPaths {
public $identifier = '%';
public $data = array();
/**
* Get all shortcodes and descriptions
*
* @return array
*/
public function get_shortcodes() {
$shortcodes = array(
'filename' => __( 'Puts in the title of the file without any spaces', 'ninja-forms-uploads' ),
'formtitle' => __( 'Puts in the title of the current form without any spaces', 'ninja-forms-uploads' ),
'date' => __( 'Puts in the date in yyyy-mm-dd (1998-05-23) format', 'ninja-forms-uploads' ),
'month' => __( 'Puts in the month in mm (04) format', 'ninja-forms-uploads' ),
'day' => __( 'Puts in the day in dd (20) format', 'ninja-forms-uploads' ),
'year' => __( 'Puts in the year in yyyy (2011) format', 'ninja-forms-uploads' ),
'username' => __( 'Puts in the user\'s username if they are logged in', 'ninja-forms-uploads' ),
'userid' => __( 'Puts in the user\'s user ID if they are logged in', 'ninja-forms-uploads' ),
'displayname' => __( 'Puts in the user\'s display name if they are logged in', 'ninja-forms-uploads' ),
'firstname' => __( 'Puts in the user\'s first name if they are logged in', 'ninja-forms-uploads' ),
'lastname' => __( 'Puts in the user\'s last name if they are logged in', 'ninja-forms-uploads' ),
'random' => __( 'Puts in a random 5 character string', 'ninja-forms-uploads' ),
);
return $shortcodes;
}
/**
* Set data to be used in the replacing of shortcodes
*
* @param string $key
* @param mixed $value
*/
public function set_data( $key, $value ) {
$this->data[$key] = sanitize_file_name( $value );
}
/**
* Get shortcode as it appears in a string
*
* @param $shortcode
*
* @return string
*/
protected function get_shortcode_display( $shortcode ) {
return $this->identifier . $shortcode . $this->identifier;
}
/**
* Replace shortcodes with values in a string
*
* @param string $string
*
* @return string
*/
public function replace_shortcodes( $string ) {
$shortcodes = self::get_shortcodes();
foreach ( $shortcodes as $shortcode => $descp ) {
$string = $this->replace_shortcode( $string, $shortcode );
}
return $string;
}
/**
* Replace the custom filename with values of fields submitted using ID and Key
*
* @param string $string
* @param array $fields
*
* @return string
*/
public function replace_field_shortcodes( $string, $fields ) {
foreach( $fields as $field ) {
$find = array();
$find[] = $this->identifier . 'field_' . $field['id'] . $this->identifier;
$key = isset( $field['key'] ) ? $field['key'] : false;
if ( ! $key ) {
$key = Ninja_Forms()->form()->get_field( $field['id'] )->get_setting( 'key' );
}
$find[] = $this->identifier . 'field_' . $key . $this->identifier;
$user_value = $field['value'];
if ( is_array( $field['value'] ) ) {
$user_value = implode( ',', $field['value'] );
}
$replace = strtolower( sanitize_file_name( trim( $user_value ) ) );
$string = str_replace( $find, $replace, $string );
}
return $string;
}
/**
* Replace a single shortcode
*
* @param string $string
* @param string $shortcode
*
* @return string
*/
public function replace_shortcode( $string, $shortcode ) {
$find = $this->get_shortcode_display( $shortcode );
$replace = $this->get_value( $shortcode );
$string = str_replace( $find, $replace, $string );
return $string;
}
/**
* Get the shortcode value
*
* @param string $shortcode
*
* @return bool|string
*/
public function get_value( $shortcode ) {
$user_mapping = array(
'username' => 'user_nicename',
'userid' => 'ID',
'displayname' => 'display_name',
'firstname' => 'user_firstname',
'lastname' => 'user_lastname',
);
if ( isset( $user_mapping[ $shortcode ] ) ) {
if ( ! is_user_logged_in() ) {
return '';
}
$current_user = wp_get_current_user();
$field = $user_mapping[ $shortcode ];
return $current_user->{$field};
}
if ( in_array( $shortcode, array( 'formtitle', 'filename' ) ) ) {
/*
* If we haven't set the source data for the replacement, just return the shortcode
* to be replaced later
*/
if ( ! isset( $this->data[ $shortcode ] ) ) {
return $this->get_shortcode_display( $shortcode );
}
}
switch ( $shortcode ) {
case 'date':
$value = date( 'Y-m-d' );
break;
case 'month':
$value = date( 'm' );
break;
case 'day':
$value = date( 'd' );
break;
case 'year':
$value = date( 'Y' );
break;
case 'random':
$value = NF_FU_Helper::random_string( 5 );
break;
case 'formtitle':
$value = sanitize_file_name( sanitize_title( trim( $this->data[ $shortcode ] ) ) );
break;
case 'filename':
$value = sanitize_file_name( sanitize_title( trim( $this->data[ $shortcode ] ) ) );
break;
default:
$value = '';
}
return strtolower( $value );
}
}