Completed
Push — master ( 1466b8...74a3d9 )
by Stephanie
14s
created

FrmFormApi::get_api_info()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 8
nop 0
dl 0
loc 33
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
class FrmFormApi {
4
5
	protected $license   = '';
6
	protected $cache_key = '';
7
	protected $cache_timeout = '+6 hours';
8
9
	/**
10
	 * @since 3.06
11
	 */
12
	public function __construct( $license = null ) {
13
		$this->set_license( $license );
14
		$this->set_cache_key();
15
	}
16
17
	/**
18
	 * @since 3.06
19
	 */
20
	private function set_license( $license ) {
21
		if ( $license === null ) {
22
			$edd_update = $this->get_pro_updater();
23
			if ( ! empty( $edd_update ) ) {
24
				$license = $edd_update->license;
25
			}
26
		}
27
		$this->license = $license;
28
	}
29
30
	/**
31
	 * @since 3.06
32
	 * @return string
33
	 */
34
	public function get_license() {
35
		return $this->license;
36
	}
37
38
	/**
39
	 * @since 3.06
40
	 */
41
	protected function set_cache_key() {
42
		$this->cache_key = 'frm_addons_l' . ( empty( $this->license ) ? '' : md5( $this->license ) );
43
	}
44
45
	/**
46
	 * @since 3.06
47
	 * @return string
48
	 */
49
	public function get_cache_key() {
50
		return $this->cache_key;
51
	}
52
53
	/**
54
	 * @since 3.06
55
	 * @return array
56
	 */
57
	public function get_api_info() {
58
		$url = $this->api_url();
59
		if ( ! empty( $this->license ) ) {
60
			$url .= '?l=' . urlencode( base64_encode( $this->license ) );
61
		}
62
63
		$addons = $this->get_cached();
64
		if ( ! empty( $addons ) ) {
65
			return $addons;
66
		}
67
68
		$response = wp_remote_get( $url );
69
		if ( is_array( $response ) && ! is_wp_error( $response ) ) {
70
		    $addons = $response['body'];
71
			if ( ! empty( $addons ) ) {
72
				$addons = json_decode( $addons, true );
73
74
				foreach ( $addons as $k => $addon ) {
75
					if ( ! isset( $addon['categories'] ) ) {
76
						continue;
77
					}
78
					$cats = array_intersect( $this->skip_categories(), $addon['categories'] );
79
					if ( ! empty( $cats ) ) {
80
						unset( $addons[ $k ] );
81
					}
82
				}
83
84
				$this->set_cached( $addons );
85
			}
86
		}
87
88
		return $addons;
89
	}
90
91
	/**
92
	 * @since 3.06
93
	 */
94
	protected function api_url() {
95
		return 'https://formidableforms.com/wp-json/s11edd/v1/updates/';
96
	}
97
98
	/**
99
	 * @since 3.06
100
	 */
101
	protected function skip_categories() {
102
		return array( 'WordPress Form Templates', 'WordPress Form Style Templates' );
103
	}
104
105
	/**
106
	 * @since 3.06
107
	 * @param object $license_plugin The FrmAddon object
108
	 * @return array
109
	 */
110
	public function get_addon_for_license( $license_plugin, $addons = array() ) {
111
		if ( empty( $addons ) ) {
112
			$addons = $this->get_api_info();
113
		}
114
		$download_id = $license_plugin->download_id;
115
		$plugin = array();
116 View Code Duplication
		if ( empty( $download_id ) && ! empty( $addons ) ) {
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...
117
			foreach ( $addons as $addon ) {
118
				if ( strtolower( $license_plugin->plugin_name ) == strtolower( $addon['title'] ) ) {
119
					return $addon;
120
				}
121
			}
122
		} elseif ( isset( $addons[ $download_id ] ) ) {
123
			$plugin = $addons[ $download_id ];
124
		}
125
126
		return $plugin;
127
	}
128
129
	/**
130
	 * @since 3.06
131
	 */
132
	public function get_pro_updater() {
133
		if ( FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) ) {
134
			$updater = FrmProAppHelper::get_updater();
135
			$this->set_license( $updater->license );
136
			return $updater;
137
		}
138
139
		return false;
140
	}
141
142
	/**
143
	 * @since 3.06
144
	 * @return array
145
	 */
146 View Code Duplication
	protected function get_cached() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
147
		$cache = get_option( $this->cache_key );
148
149
		if ( empty( $cache ) || empty( $cache['timeout'] ) || current_time( 'timestamp' ) > $cache['timeout'] ) {
150
			return false; // Cache is expired
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by FrmFormApi::get_cached of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
151
		}
152
153
		return json_decode( $cache['value'], true );
154
	}
155
156
	/**
157
	 * @since 3.06
158
	 */
159
	protected function set_cached( $addons ) {
160
		$data = array(
161
			'timeout' => strtotime( $this->cache_timeout, current_time( 'timestamp' ) ),
162
			'value'   => json_encode( $addons ),
163
		);
164
165
		update_option( $this->cache_key, $data, 'no' );
166
	}
167
168
	/**
169
	 * @since 3.06
170
	 */
171
	public function reset_cached() {
172
		delete_option( $this->cache_key );
173
	}
174
175
	/**
176
	 * @since 3.06
177
	 * @return array
178
	 */
179
	public function error_for_license() {
180
		$errors = array();
181
		if ( ! empty( $this->license ) ) {
182
			$errors = $this->get_error_from_response();
183
		}
184
		return $errors;
185
	}
186
187
	/**
188
	 * @since 3.06
189
	 * @return array
190
	 */
191
	public function get_error_from_response( $addons = array() ) {
192
		if ( empty( $addons ) ) {
193
			$addons = $this->get_api_info();
194
		}
195
		$errors = array();
196
		if ( isset( $addons['error'] ) ) {
197
			$errors[] = $addons['error']['message'];
198
			do_action( 'frm_license_error', $addons['error'] );
199
		}
200
		return $errors;
201
	}
202
}
203