| 1 |
<?php |
| 2 |
|
| 3 |
abstract class Flamingo_CSV { |
| 4 |
|
| 5 |
public function get_file_name() { |
| 6 |
return 'flamingo.csv'; |
| 7 |
} |
| 8 |
|
| 9 |
public function send_http_headers() { |
| 10 |
$filename = $this->get_file_name(); |
| 11 |
$charset = get_option( 'blog_charset' ); |
| 12 |
|
| 13 |
header( "Content-Description: File Transfer" ); |
| 14 |
header( "Content-Disposition: attachment; filename=$filename" ); |
| 15 |
header( "Content-Type: text/csv; charset=$charset" ); |
| 16 |
} |
| 17 |
|
| 18 |
public function print_data() { |
| 19 |
echo ''; |
| 20 |
} |
| 21 |
|
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
class Flamingo_Contact_CSV extends Flamingo_CSV { |
| 26 |
|
| 27 |
public function get_file_name() { |
| 28 |
return sprintf( |
| 29 |
'%1$s-flamingo-contact-%2$s.csv', |
| 30 |
sanitize_key( get_bloginfo( 'name' ) ), |
| 31 |
wp_date( 'Y-m-d' ) |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
public function print_data() { |
| 36 |
$labels = array( |
| 37 |
__( 'Email', 'flamingo' ), |
| 38 |
__( 'Full name', 'flamingo' ), |
| 39 |
__( 'First name', 'flamingo' ), |
| 40 |
__( 'Last name', 'flamingo' ), |
| 41 |
); |
| 42 |
|
| 43 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 44 |
echo flamingo_csv_row( $labels ); |
| 45 |
|
| 46 |
$args = array( |
| 47 |
'posts_per_page' => -1, |
| 48 |
'orderby' => 'meta_value', |
| 49 |
'order' => 'ASC', |
| 50 |
'meta_key' => '_email', |
| 51 |
); |
| 52 |
|
| 53 |
if ( ! empty( $_GET['s'] ) ) { |
| 54 |
$args['s'] = $_GET['s']; |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! empty( $_GET['orderby'] ) ) { |
| 58 |
if ( 'email' === $_GET['orderby'] ) { |
| 59 |
$args['meta_key'] = '_email'; |
| 60 |
} elseif ( 'name' === $_GET['orderby'] ) { |
| 61 |
$args['meta_key'] = '_name'; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if ( |
| 66 |
! empty( $_GET['order'] ) and |
| 67 |
'asc' === strtolower( $_GET['order'] ) |
| 68 |
) { |
| 69 |
$args['order'] = 'ASC'; |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! empty( $_GET['contact_tag_id'] ) ) { |
| 73 |
$args['contact_tag_id'] = explode( ',', $_GET['contact_tag_id'] ); |
| 74 |
} |
| 75 |
|
| 76 |
$items = Flamingo_Contact::find( $args ); |
| 77 |
|
| 78 |
foreach ( $items as $item ) { |
| 79 |
echo "\r\n"; |
| 80 |
|
| 81 |
$row = array( |
| 82 |
$item->email, |
| 83 |
$item->get_prop( 'name' ), |
| 84 |
$item->get_prop( 'first_name' ), |
| 85 |
$item->get_prop( 'last_name' ), |
| 86 |
); |
| 87 |
|
| 88 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 89 |
echo flamingo_csv_row( $row ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
class Flamingo_Inbound_CSV extends Flamingo_CSV { |
| 97 |
|
| 98 |
public function get_file_name() { |
| 99 |
return sprintf( |
| 100 |
'%1$s-flamingo-inbound-%2$s.csv', |
| 101 |
sanitize_key( get_bloginfo( 'name' ) ), |
| 102 |
wp_date( 'Y-m-d' ) |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
public function print_data() { |
| 107 |
$args = array( |
| 108 |
'posts_per_page' => -1, |
| 109 |
'orderby' => 'date', |
| 110 |
'order' => 'DESC', |
| 111 |
); |
| 112 |
|
| 113 |
if ( ! empty( $_REQUEST['s'] ) ) { |
| 114 |
$args['s'] = $_REQUEST['s']; |
| 115 |
} |
| 116 |
|
| 117 |
if ( ! empty( $_REQUEST['orderby'] ) ) { |
| 118 |
if ( 'subject' === $_REQUEST['orderby'] ) { |
| 119 |
$args['meta_key'] = '_subject'; |
| 120 |
$args['orderby'] = 'meta_value'; |
| 121 |
} elseif ( 'from' === $_REQUEST['orderby'] ) { |
| 122 |
$args['meta_key'] = '_from'; |
| 123 |
$args['orderby'] = 'meta_value'; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if ( |
| 128 |
! empty( $_REQUEST['order'] ) and |
| 129 |
'asc' === strtolower( $_REQUEST['order'] ) |
| 130 |
) { |
| 131 |
$args['order'] = 'ASC'; |
| 132 |
} |
| 133 |
|
| 134 |
if ( ! empty( $_REQUEST['m'] ) ) { |
| 135 |
$args['m'] = $_REQUEST['m']; |
| 136 |
} |
| 137 |
|
| 138 |
if ( ! empty( $_REQUEST['channel_id'] ) ) { |
| 139 |
$args['channel_id'] = $_REQUEST['channel_id']; |
| 140 |
} |
| 141 |
|
| 142 |
if ( ! empty( $_REQUEST['channel'] ) ) { |
| 143 |
$args['channel'] = $_REQUEST['channel']; |
| 144 |
} |
| 145 |
|
| 146 |
$items = Flamingo_Inbound_Message::find( $args ); |
| 147 |
|
| 148 |
if ( empty( $items ) ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
$labels = array_keys( $items[0]->fields ); |
| 153 |
|
| 154 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 155 |
echo flamingo_csv_row( |
| 156 |
array_merge( $labels, array( __( 'Date', 'flamingo' ) ) ) |
| 157 |
); |
| 158 |
|
| 159 |
foreach ( $items as $item ) { |
| 160 |
echo "\r\n"; |
| 161 |
|
| 162 |
$row = array(); |
| 163 |
|
| 164 |
foreach ( $labels as $label ) { |
| 165 |
$col = isset( $item->fields[$label] ) ? $item->fields[$label] : ''; |
| 166 |
|
| 167 |
if ( is_array( $col ) ) { |
| 168 |
$col = flamingo_array_flatten( $col ); |
| 169 |
$col = array_filter( array_map( 'trim', $col ) ); |
| 170 |
$col = implode( ', ', $col ); |
| 171 |
} |
| 172 |
|
| 173 |
$row[] = $col; |
| 174 |
} |
| 175 |
|
| 176 |
$row[] = get_post_time( 'c', false, $item->id() ); // Date |
| 177 |
|
| 178 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 179 |
echo flamingo_csv_row( $row ); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* Retrieves text that represents a CSV row. |
| 188 |
*/ |
| 189 |
function flamingo_csv_row( $inputs = array() ) { |
| 190 |
$row = array(); |
| 191 |
|
| 192 |
foreach ( $inputs as $input ) { |
| 193 |
$row[] = apply_filters( 'flamingo_csv_quotation', $input ); |
| 194 |
} |
| 195 |
|
| 196 |
$separator = apply_filters( 'flamingo_csv_value_separator', ',' ); |
| 197 |
|
| 198 |
return implode( $separator, $row ); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
add_filter( 'flamingo_csv_quotation', 'flamingo_csv_quote', 10, 1 ); |
| 203 |
|
| 204 |
/** |
| 205 |
* Retrieves text that represents a CSV cell with quotation. |
| 206 |
*/ |
| 207 |
function flamingo_csv_quote( $input ) { |
| 208 |
$input = trim( $input ); |
| 209 |
|
| 210 |
$prefix = apply_filters( 'flamingo_csv_field_prefix', '', $input ); |
| 211 |
|
| 212 |
$input = trim( sprintf( '%1$s %2$s', $prefix, $input ) ); |
| 213 |
|
| 214 |
return sprintf( '"%s"', str_replace( '"', '""', $input ) ); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
add_filter( 'flamingo_csv_field_prefix', |
| 219 |
'flamingo_csv_field_prefix_text', |
| 220 |
10, 2 |
| 221 |
); |
| 222 |
|
| 223 |
/** |
| 224 |
* Adds a security alert at the head of a cell. |
| 225 |
* |
| 226 |
* @see https://contactform7.com/2020/01/15/heads-up-about-spreadsheet-vulnerabilities/ |
| 227 |
*/ |
| 228 |
function flamingo_csv_field_prefix_text( $prefix, $input ) { |
| 229 |
$formula_triggers = array( '=', '+', '-', '@' ); |
| 230 |
|
| 231 |
if ( in_array( substr( $input, 0, 1 ), $formula_triggers, true ) ) { |
| 232 |
/* translators: %s: URL */ |
| 233 |
$prefix = __( '(Security Alert: Suspicious content is detected. See %s for details.)', 'flamingo' ); |
| 234 |
|
| 235 |
if ( in_array( substr( $prefix, 0, 1 ), $formula_triggers, true ) ) { |
| 236 |
$prefix = '\'' . $prefix; |
| 237 |
} |
| 238 |
|
| 239 |
$prefix = sprintf( |
| 240 |
$prefix, |
| 241 |
esc_url( __( 'https://contactform7.com/heads-up-about-spreadsheet-vulnerabilities', 'flamingo' ) ) |
| 242 |
); |
| 243 |
} |
| 244 |
|
| 245 |
return $prefix; |
| 246 |
} |
| 247 |
|