Completed
Push — master ( de6a17...35dc2a )
by Sam
02:15
created

ControllerBase::set_filesystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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
Coding Style introduced by Sam Wilson
verify_nonce uses the super-global variable $_REQUEST which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
80
		$nonce = wp_verify_nonce( $_REQUEST['_wpnonce'], $action );
81
		if ( ! $nonce ) {
82
			wp_die();
83
		}
84
	}
85
}
86