These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file contains only a single class. |
||
4 | * |
||
5 | * @package Tabulate |
||
6 | * @file |
||
7 | */ |
||
8 | |||
9 | namespace WordPress\Tabulate\Controllers; |
||
10 | |||
11 | /** |
||
12 | * All controllers must inherit from this class. |
||
13 | */ |
||
14 | abstract class ControllerBase { |
||
15 | |||
16 | /** |
||
17 | * The global database object. |
||
18 | * |
||
19 | * @var \wpdb |
||
20 | */ |
||
21 | protected $wpdb; |
||
22 | |||
23 | /** |
||
24 | * The injected $_GET query string. |
||
25 | * |
||
26 | * @var string[] |
||
27 | */ |
||
28 | protected $get; |
||
29 | |||
30 | /** |
||
31 | * The global filesystem object. |
||
32 | * |
||
33 | * @var \WP_Filesystem_Base |
||
34 | */ |
||
35 | protected $filesystem; |
||
36 | |||
37 | /** |
||
38 | * Create the controller, with the global database and query string. |
||
39 | * |
||
40 | * @param \wpdb $wpdb The global wpdb object. |
||
41 | * @param string[] $get The $_GET array. |
||
42 | */ |
||
43 | public function __construct( $wpdb, $get = array() ) { |
||
44 | $this->wpdb = $wpdb; |
||
45 | $this->get = $get; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Set the filesystem. |
||
50 | * |
||
51 | * @param \WP_Filesystem_Base $filesystem The filesystem object. |
||
52 | */ |
||
53 | public function set_filesystem( \WP_Filesystem_Base $filesystem ) { |
||
54 | $this->filesystem = $filesystem; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Send specified content to the client as a downloadable file. |
||
59 | * |
||
60 | * @param string $ext The file extension. |
||
61 | * @param string $mime The mime-type of the file. |
||
62 | * @param string $content The file's contents. |
||
63 | * @param string|boolean $download_name The name of the file, for the user to download. |
||
64 | */ |
||
65 | protected function send_file( $ext, $mime, $content, $download_name = false ) { |
||
66 | $download_name = ($download_name ?: date( 'Y-m-d' ) ) . '.' . $ext; |
||
67 | header( 'Content-Encoding: UTF-8' ); |
||
68 | header( 'Content-type: ' . $mime . '; charset=UTF-8' ); |
||
69 | header( 'Content-Disposition: attachment; filename="' . $download_name . '"' ); |
||
70 | echo $content; |
||
71 | exit; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Verify the _wpnonce request value. |
||
76 | * |
||
77 | * @param string $action The action name. |
||
78 | */ |
||
79 | public function verify_nonce( $action ) { |
||
0 ignored issues
–
show
|
|||
80 | $nonce = wp_verify_nonce( $_REQUEST['_wpnonce'], $action ); |
||
81 | if ( ! $nonce ) { |
||
82 | wp_die(); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: