Completed
Push — develop ( 79527a...a01b60 )
by
unknown
04:31 queued 02:08
created

Wordlift_Shortcode_REST   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 111
Duplicated Lines 17.12 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 19
loc 111
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 19 38 7
get_data() 0 1 ?
B rest_callback() 0 46 5
A is_endpoint() 0 14 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
use Wordlift\Cache\Ttl_Cache;
4
5
/**
6
 * A base abstract class for shortcode REST backend which does
7
 * some common tasks.
8
 *
9
 * @since 3.5.4
10
 */
11
abstract class Wordlift_Shortcode_REST {
12
13
	/**
14
	 * The cache_ttl, set by extending classes.
15
	 */
16
	const CACHE_TTL = 86400; // 24 hours
17
18
	public function __construct( $endpoint, $args ) {
19
20
		$scope          = $this;
21
		$this->endpoint = $endpoint;
0 ignored issues
show
Bug introduced by
The property endpoint does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
		$this->args     = $args;
0 ignored issues
show
Bug introduced by
The property args does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
24
		// Register rest route with callback
25
		add_action( 'rest_api_init', function () use ( $scope ) {
26
			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $scope->endpoint, array(
27
				'methods'  => WP_REST_Server::READABLE,
28
				'callback' => array( $scope, 'rest_callback' ),
29
				'args'     => $scope->args
30
			) );
31
		} );
32
33
		// Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit.
34 View Code Duplication
		add_action( 'plugins_loaded', function () use ( $scope ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
36
			if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
37
				return;
38
			}
39
40
			remove_action( 'plugins_loaded', 'rocket_init' );
41
			remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
42
			remove_action( 'plugins_loaded', 'wpseo_init', 14 );
43
		}, 0 );
44
45 View Code Duplication
		add_action( 'init', function () use ( $scope ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
47
			if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
48
				return;
49
			}
50
51
			remove_action( 'init', 'wp_widgets_init', 1 );
52
			remove_action( 'init', 'gglcptch_init' );
53
		}, 0 );
54
55
	}
56
57
	public abstract function get_data( $request );
58
59
	public function rest_callback( WP_REST_Request $request ) {
60
61
		// Respond from origin if TTL is 0
62
		if ( static::CACHE_TTL == 0 ) {
63
64
			$data     = $this->get_data( $request );
65
			$response = rest_ensure_response( $data );
66
			if ( is_wp_error( $data ) ) {
67
				return $response;
68
			}
69
			$response->header( 'X-WordLift-Cache', 'MISS' );
70
71
			return $response;
72
		}
73
74
		// Create the cache key.
75
		$cache_key_params = $request->get_params();
76
		unset( $cache_key_params['uniqid'] );
77
		unset( $cache_key_params['rest_route'] );
78
		$cache_key = array( 'request_params' => $cache_key_params );
79
80
		// Create the TTL cache and try to get the results.
81
		$cache         = new Ttl_Cache( $this->endpoint, static::CACHE_TTL );
82
		$cache_results = $cache->get( $cache_key );
83
84
		if ( isset( $cache_results ) ) {
85
86
			$response = rest_ensure_response( $cache_results );
87
			$response->header( 'X-WordLift-Cache', 'HIT' );
88
89
			return $response;
90
		}
91
92
		$data     = $this->get_data( $request );
93
		$response = rest_ensure_response( $data );
94
		if ( is_wp_error( $data ) ) {
95
			return $response;
96
		}
97
		$response->header( 'X-WordLift-Cache', 'MISS' );
98
99
		// Put the result before sending the json to the client, since sending the json will terminate us.
100
		$cache->put( $cache_key, $data );
101
102
		return $response;
103
104
	}
105
106
	private function is_endpoint() {
107
		$compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint;
108
109
		// Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably
110
111
		if ( $_SERVER['REQUEST_URI'] && strpos( $_SERVER['REQUEST_URI'], $compare_route ) ) {
112
			return true;
113
		}
114
		if ( ! empty( $_GET['rest_route'] ) && strpos( $_GET['rest_route'], $compare_route ) ) {
115
			return true;
116
		}
117
118
		return false;
119
	}
120
121
}
122