1 | <?php |
||
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() ) { |
||
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 ) { |
||
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 ) { |
||
73 | |||
74 | /** |
||
75 | * Verify the _wpnonce request value. |
||
76 | * |
||
77 | * @param string $action The action name. |
||
78 | */ |
||
79 | public function verify_nonce( $action ) { |
||
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: