1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* WP Enerscore API (http://map.enerscore.com/#/api) |
4
|
|
|
* |
5
|
|
|
* @package WP-Enerscore-API |
6
|
|
|
*/ |
7
|
|
|
/* |
8
|
|
|
Plugin Name: WP Enerscore API |
9
|
|
|
Plugin URI: https://github.com/wp-api-libraries/wp-enerscore-api |
10
|
|
|
Description: Perform API requests to enerscore.com in WordPress. |
11
|
|
|
Author: imFORZA |
12
|
|
|
Version: 1.0.0 |
13
|
|
|
Author URI: https://www.imforza.com |
14
|
|
|
GitHub Plugin URI: https://github.com/wp-api-libraries/wp-enerscore-api |
15
|
|
|
GitHub Branch: master |
16
|
|
|
*/ |
17
|
|
|
/* Exit if accessed directly */ |
18
|
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; } |
19
|
|
|
/* Check if class exists. */ |
20
|
|
|
if ( ! class_exists( 'EnerscoreAPI' ) ) { |
21
|
|
|
/** |
22
|
|
|
* EnerscoreAPI class. |
23
|
|
|
*/ |
24
|
|
|
class EnerscoreAPI { |
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* URL to the API. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $base_uri = 'http://api-alpha.enerscore.com/api/address/neighbors/'; |
31
|
|
|
/** |
32
|
|
|
* __construct function. |
33
|
|
|
* |
34
|
|
|
* @access public |
35
|
|
|
* @return void |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function __construct() { |
38
|
|
|
} |
39
|
|
|
/** |
40
|
|
|
* Fetch the request from the API. |
41
|
|
|
* |
42
|
|
|
* @access private |
43
|
|
|
* @param mixed $request Request URL. |
44
|
|
|
* @return $body Body. |
|
|
|
|
45
|
|
|
*/ |
46
|
|
View Code Duplication |
private function fetch( $request ) { |
|
|
|
|
47
|
|
|
$response = wp_remote_get( $request ); |
48
|
|
|
$code = wp_remote_retrieve_response_code( $response ); |
49
|
|
|
if ( 200 !== $code ) { |
50
|
|
|
return new WP_Error( 'response-error', sprintf( __( 'Server response code: %d', 'text-domain' ), $code ) ); |
51
|
|
|
} |
52
|
|
|
$body = wp_remote_retrieve_body( $response ); |
53
|
|
|
return json_decode( $body ); |
54
|
|
|
} |
55
|
|
|
/** |
56
|
|
|
* Get Energy Score for Address. |
57
|
|
|
* |
58
|
|
|
* @access public |
59
|
|
|
* @param mixed $address Address. |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function get_enerscore( $address ) { |
63
|
|
|
if ( empty( $address ) ) { |
64
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 'text-domain' ) ); |
65
|
|
|
} |
66
|
|
|
$request = $this->base_uri . $address; |
67
|
|
|
return $this->fetch( $request ); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.