Completed
Push — master ( ce5918...20c3c4 )
by David
02:48
created

Wordlift_Http_Api   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A add_rewrite_endpoint() 0 6 1
A template_redirect() 0 15 2
B do_action() 0 29 3
A nopriv_hello_world() 0 5 1
A hello_world() 0 5 1
A ensure_rewrite_rules_are_flushed() 0 8 2
1
<?php
2
/**
3
 * Service: Http Api.
4
 *
5
 * Handle calls to `/wl-api`.
6
 *
7
 * See https://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/.
8
 *
9
 * @since 3.15.3
10
 */
11
12
/**
13
 * Define the {@link Wordlift_Http_Api} class.
14
 *
15
 * @since 3.15.3
16
 */
17
class Wordlift_Http_Api {
18
19
	/**
20
	 * A {@link Wordlift_Log_Service} instance.
21
	 *
22
	 * @since 3.15.3
23
	 *
24
	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
25
	 */
26
	private $log;
27
28
	/**
29
	 * Create a {@link Wordlift_End_Point} instance.
30
	 *
31
	 * @since 3.15.3
32
	 */
33
	public function __construct() {
34
35
		$this->log = Wordlift_Log_Service::get_logger( get_class() );
36
37
		add_action( 'init', array( $this, 'add_rewrite_endpoint' ) );
38
		add_action( 'template_redirect', array( $this, 'template_redirect' ) );
39
		add_action( 'admin_post_wl_hello_world', array( $this, 'hello_world' ) );
40
		add_action( 'admin_post_nopriv_wl_hello_world', array( $this, 'nopriv_hello_world' ) );
41
42
	}
43
44
	/**
45
	 * Add the `wl-api` rewrite end-point.
46
	 *
47
	 * @since 3.15.3
48
	 */
49
	public function add_rewrite_endpoint() {
50
51
		add_rewrite_endpoint( 'wl-api', EP_ROOT );
52
		$this->ensure_rewrite_rules_are_flushed();
53
54
	}
55
56
	/**
57
	 * Handle `template_redirect` hooks.
58
	 *
59
	 * @since 3.15.3
60
	 */
61
	public function template_redirect() {
0 ignored issues
show
Coding Style introduced by
template_redirect 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...
62
63
		global $wp_query;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
64
65
		if ( ! isset( $wp_query->query_vars['wl-api'] ) ) {
66
			$this->log->trace( 'Skipping, not a `wl-api` call.' );
67
68
			return;
69
		}
70
71
		$this->do_action( $_REQUEST['action'] );
72
73
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method template_redirect() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
74
75
	}
76
77
	/**
78
	 * Do the requested action.
79
	 *
80
	 * @since 3.15.3
81
	 *
82
	 * @param string $action The action to execute.
83
	 */
84
	private function do_action( $action ) {
85
86
		if ( empty( $action ) ) {
87
			return;
88
		}
89
90
		if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) {
91
			/**
92
			 * Fires on a non-authenticated admin post request for the given action.
93
			 *
94
			 * The dynamic portion of the hook name, `$action`, refers to the given
95
			 * request action.
96
			 *
97
			 * @since 2.6.0
98
			 */
99
			do_action( "admin_post_nopriv_{$action}" );
100
		} else {
101
			/**
102
			 * Fires on an authenticated admin post request for the given action.
103
			 *
104
			 * The dynamic portion of the hook name, `$action`, refers to the given
105
			 * request action.
106
			 *
107
			 * @since 2.6.0
108
			 */
109
			do_action( "admin_post_{$action}" );
110
		}
111
112
	}
113
114
	/**
115
	 * Test function, anonymous.
116
	 *
117
	 * @since 3.15.3
118
	 */
119
	public function nopriv_hello_world() {
120
121
		wp_die( 'Hello World! (from anonymous)' );
122
123
	}
124
125
	/**
126
	 * Test function, authenticated.
127
	 *
128
	 * @since 3.15.3
129
	 */
130
	public function hello_world() {
131
132
		wp_die( 'Hello World! (from authenticated)' );
133
134
	}
135
136
	/**
137
	 * Ensure that the rewrite rules are flushed the first time.
138
	 *
139
	 * @since 3.15.3
140
	 */
141
	public static function ensure_rewrite_rules_are_flushed() {
142
143
		if ( 1 !== get_option( 'wl_http_api' ) ) {
144
			flush_rewrite_rules();
145
			add_option( 'wl_http_api', 1 );
146
		}
147
148
	}
149
150
}
151