C:\xampp\htdocs\landing\wp-content\plugins\hummingbird-performance\core\api\class-rest.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
 * Manage Hummingbird REST API endpoints
 *
 * @package Hummingbird\Core\Api
 */

namespace Hummingbird\Core\Api;

use 
Hummingbird\Core\Utils;
use 
WP_Error;
use 
WP_REST_Request;
use 
WP_REST_Server;

if ( ! 
defined'ABSPATH' ) ) {
    exit;
}

/**
 * Class REST
 */
class Rest {

    
/**
     * REST API version.
     *
     * @var string
     */
    
public $version '1';

    
/**
     * REST API namespace.
     *
     * @var string
     */
    
public $namespace 'hummingbird';

    
/**
     * REST constructor.
     */
    
public function __construct() {
        
add_action'rest_api_init', array( $this'register_routes' ) );
    }

    
/**
     * Get namespace with version.
     *
     * @return string
     */
    
protected function get_namespace() {
        return 
$this->namespace '/v' $this->version;
    }

    
/**
     * Register the REST routes.
     */
    
public function register_routes() {
        
// Route to return a modules status.
        
register_rest_route(
            
$this->get_namespace(),
            
'/status/(?P<module>[\\w-]+)',
            array(
                
'methods'             => WP_REST_Server::READABLE,
                
'callback'            => array( $this'get_module_status' ),
                
'permission_callback' => '__return_true',
                
'args'                => array(
                    
'module' => array(
                        
'required'          => true,
                        
'sanitize_callback' => 'sanitize_key',
                    ),
                ),
            )
        );

        
// Route to clear a modules cache.
        
register_rest_route(
            
$this->get_namespace(),
            
'/clear_cache/(?P<module>[\\w-]+)',
            array(
                
'methods'             => WP_REST_Server::READABLE,
                
'callback'            => array( $this'clear_module_cache' ),
                
'permission_callback' => '__return_true',
                
'module'              => array(
                    
'required'          => true,
                    
'sanitize_callback' => 'sanitize_key',
                ),
            )
        );

        
// Test route used to check if API is working.
        
register_rest_route(
            
$this->get_namespace(),
            
'/test',
            array(
                
'methods'             => 'POST,GET,PUT,PATCH,DELETE,COPY,HEAD',
                
'callback'            => function() {
                    return 
true;
                },
                
'permission_callback' => '__return_true',
            )
        );
    }

    
/**
     * Returns the status of a module.
     *
     * @param WP_REST_Request $request  Request.
     * @return mixed
     */
    
public function get_module_status$request ) {
        
$module $request->get_param'module' );

        
$available_modules = array(
            
'gzip',
            
'caching',
        );
        if ( ! 
in_array$module$available_modulestrue ) ) {
            return new 
WP_Error(
                
'invalid_module',
                
__'The requested module status was invalid.''wphb' ),
                array(
                    
'status' => 400,
                )
            );
        }

        
$response = array(
            
'module_active' => Utils::get_module$module )->is_active(),
            
'data'          => Utils::get_module$module )->analyze_data(),
        );

        return 
rest_ensure_response$response );
    }

    
/**
     * Clears the cache of a module.
     *
     * @param WP_REST_Request $request  Request.
     * @return mixed
     */
    
public function clear_module_cache$request ) {
        
$module            $request->get_param'module' );
        
$available_modules = array(
            
'page_cache',
            
'performance',
            
'gravatar',
            
'minify',
            
'cloudflare',
        );

        
// Make sure modules cache can be cleared.
        
if ( ! in_array$module$available_modulestrue ) ) {
            return new 
WP_Error(
                
'invalid_module',
                
__'The requested module was invalid.''wphb' ),
                array(
                    
'status' => 400,
                )
            );
        }

        
// Make sure module is active.
        
if ( ! Utils::get_module$module )->is_active() ) {
            return new 
WP_Error(
                
'inactive_module',
                
__'The requested module is inactive.''wphb' ),
                array(
                    
'status' => 400,
                )
            );
        }

        
// Clear the cache of module.
        
switch ( $module ) {
            case 
'minify':
                
$response = array(
                    
'cache_cleared' => Utils::get_module$module )->clear_cachefalse ),
                );
                break;
            default:
                
$response = array(
                    
'cache_cleared' => Utils::get_module$module )->clear_cache(),
                );
                break;
        }

        return 
rest_ensure_response$response );
    }

}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586