Completed
Push — master ( c2ab66...fc2fb1 )
by David
03:14
created

Wordlift_Http_Api   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 162
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A add_rewrite_endpoint() 0 6 1
A template_redirect() 0 15 2
A 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 11 2
A activate() 0 6 1
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
40
		//region SAMPLE ACTIONS.
41
		add_action( 'admin_post_wl_hello_world', array(
42
			$this,
43
			'hello_world',
44
		) );
45
		add_action( 'admin_post_nopriv_wl_hello_world', array(
46
			$this,
47
			'nopriv_hello_world',
48
		) );
49
		//endregion
50
51
	}
52
53
	/**
54
	 * Add the `wl-api` rewrite end-point.
55
	 *
56
	 * @since 3.15.3
57
	 */
58
	public function add_rewrite_endpoint() {
59
60
		add_rewrite_endpoint( 'wl-api', EP_ROOT );
61
		$this->ensure_rewrite_rules_are_flushed();
62
63
	}
64
65
	/**
66
	 * Handle `template_redirect` hooks.
67
	 *
68
	 * @since 3.15.3
69
	 */
70
	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...
71
72
		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...
73
74
		if ( ! isset( $wp_query->query_vars['wl-api'] ) ) {
75
			$this->log->trace( 'Skipping, not a `wl-api` call.' );
76
77
			return;
78
		}
79
80
		$this->do_action( $_REQUEST['action'] );
81
82
		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...
83
84
	}
85
86
	/**
87
	 * Do the requested action.
88
	 *
89
	 * @since 3.15.3
90
	 *
91
	 * @param string $action The action to execute.
92
	 */
93
	private function do_action( $action ) {
94
95
		if ( empty( $action ) ) {
96
			return;
97
		}
98
99
		if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) {
100
			/**
101
			 * Fires on a non-authenticated admin post request for the given action.
102
			 *
103
			 * The dynamic portion of the hook name, `$action`, refers to the given
104
			 * request action.
105
			 *
106
			 * @since 2.6.0
107
			 */
108
			do_action( "admin_post_nopriv_{$action}" );
109
		} else {
110
			/**
111
			 * Fires on an authenticated admin post request for the given action.
112
			 *
113
			 * The dynamic portion of the hook name, `$action`, refers to the given
114
			 * request action.
115
			 *
116
			 * @since 2.6.0
117
			 */
118
			do_action( "admin_post_{$action}" );
119
		}
120
121
	}
122
123
	/**
124
	 * Test function, anonymous.
125
	 *
126
	 * @since 3.15.3
127
	 */
128
	public function nopriv_hello_world() {
129
130
		wp_die( 'Hello World! (from anonymous)' );
131
132
	}
133
134
	/**
135
	 * Test function, authenticated.
136
	 *
137
	 * @since 3.15.3
138
	 */
139
	public function hello_world() {
140
141
		wp_die( 'Hello World! (from authenticated)' );
142
143
	}
144
145
	/**
146
	 * Ensure that the rewrite rules are flushed the first time.
147
	 *
148
	 * @since 3.16.0 changed the value from 1 to `yes` to avoid type juggling issues.
149
	 * @since 3.15.3
150
	 */
151
	public static function ensure_rewrite_rules_are_flushed() {
152
153
		// See https://github.com/insideout10/wordlift-plugin/issues/698.
154
		if ( 'yes' !== get_option( 'wl_http_api' ) ) {
155
			update_option( 'wl_http_api', 'yes' );
156
			add_action( 'wp_loaded', function () {
157
				flush_rewrite_rules();
158
			} );
159
		}
160
161
	}
162
163
	/**
164
	 * Called by {@see activate_wordlift}, resets the `wl_http_api` option flag in order to force WordLift to
165
	 * reinitialize the `wl-api` route.
166
	 *
167
	 * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue.
168
	 *
169
	 * @since 3.19.2
170
	 */
171
	public static function activate() {
172
173
		// Force the plugin to reinitialize the rewrite rules.
174
		update_option( 'wl_http_api', 'no' );
175
176
	}
177
178
}
179