PluginProbe
Property Hive / trunk
Property Hive vtrunk
2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 1.4.63 All 259 releases
propertyhive / includes / admin / class-ph-admin-plugin-updates.php

class-ph-admin-plugin-updates.php in Property Hive trunk, at includes/admin/class-ph-admin-plugin-updates.php

224 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Manages Property Hive plugin updating on the Plugins screen, showing warning and messages where applicable such as key updates which might break things
4 *
5 * @package PropertyHive/Admin
6 * @version 1.0.0
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13
14 /**
15 * Class PH_Plugin_Updates
16 */
17 class PH_Plugin_Updates {
18
19 /**
20 * The upgrade notice shown inline.
21 *
22 * @var string
23 */
24 protected $upgrade_notice = '';
25
26 /**
27 * Constructor.
28 */
29 public function __construct() {
30 add_action( 'in_plugin_update_message-propertyhive/propertyhive.php', array( $this, 'in_plugin_update_message' ), 10, 2 );
31
32 if ( is_network_admin() || ! is_multisite() )
33 {
34 $license_type = PH()->license->get_license_type();
35
36 $valid_license = false;
37
38 if ( $license_type == 'old' )
39 {
40 $license = PH()->license->get_current_license();
41
42 if ( is_array($license) && empty($license) && get_option( 'propertyhive_license_key', '' ) != '' )
43 {
44
45 }
46 elseif ( isset($license['active']) && $license['active'] != '1' )
47 {
48
49 }
50 else
51 {
52 if ( isset($license['expires_at']) && $license['expires_at'] != '' )
53 {
54 if ( strtotime($license['expires_at']) <= time() )
55 {
56 // Expired
57
58 }
59 else
60 {
61 // Valid
62 $valid_license = true;
63 }
64 }
65 }
66 }
67 elseif ( $license_type == 'pro' )
68 {
69 // get new license information
70 if ( get_option('propertyhive_pro_license_key', '') != '' )
71 {
72 if ( PH()->license->is_valid_pro_license_key() )
73 {
74 $valid_license = true;
75 }
76 }
77 }
78
79 if ( !$valid_license )
80 {
81 $add_ons = '';
82 if ( false === ( $add_ons = get_transient( 'propertyhive_features' ) ) || isset($_GET['ph_force_get_features']) )
83 {
84 // It wasn't there, so regenerate the data and save the transient
85 $response = wp_remote_get(
86 'https://wp-property-hive.com/add-ons-json.php',
87 array(
88 'timeout' => 10
89 )
90 );
91
92 if ( !is_wp_error( $response ) && is_array( $response ) )
93 {
94 $body = $response['body']; // use the content
95
96 $add_ons = json_decode($body, TRUE);
97
98 if ( $add_ons !== FALSE && is_array($add_ons) && !empty($add_ons) )
99 {
100 set_transient( 'propertyhive_features', $add_ons, DAY_IN_SECONDS );
101 }
102 }
103 }
104
105 if ($add_ons !== FALSE && !empty($add_ons))
106 {
107 foreach ($add_ons as $add_on)
108 {
109 $url = trim($add_on['url'], '/');
110
111 if (
112 isset($add_on['wordpress_plugin_file']) && $add_on['wordpress_plugin_file'] != '' && $add_on['wordpress_plugin_file'] != false &&
113 isset($add_on['wordpress_version_option_name']) && $add_on['wordpress_version_option_name'] != '' && $add_on['wordpress_version_option_name'] != false
114 )
115 {
116 $plugin_file = $add_on['wordpress_plugin_file'];
117
118 if ( is_plugin_active($plugin_file) )
119 {
120 $current_version = get_option($add_on['wordpress_version_option_name'], '');
121
122 if ( $current_version != '' )
123 {
124 // check if add on update available
125 $new_version = $add_on['version'];
126
127 if ( version_compare($new_version, $current_version) == 1 )
128 {
129 add_action( 'after_plugin_row_' . $plugin_file, array( $this, 'test' ), 10, 3 );
130 }
131 }
132 }
133 }
134 }
135 }
136 }
137 }
138 }
139
140 public function test( $plugin_file, $plugin_data, $status )
141 {
142 if ( is_network_admin() ) {
143 $active_class = is_plugin_active_for_network( $plugin_file ) ? ' active' : '';
144 } else {
145 $active_class = is_plugin_active( $plugin_file ) ? ' active' : '';
146 }
147
148 echo '<tr class="plugin-update-tr' . esc_attr($active_class) . '">
149 <td colspan="3" class="plugin-update colspanchange">
150 <div class="update-message notice inline notice-warning notice-alt">
151 <p>An update is available for this plugin. It is recommended that you update to ensure you receive the latest features and bug fixes. You can access updates to plugins by <a href="https://wp-property-hive.com/pricing/?src=plugin-update-notice" target="_blank">purchasing a pro package</a>.</p>
152 </div>
153 </td>
154 </tr>';
155 }
156
157 /**
158 * Show plugin changes on the plugins screen.
159 *
160 * @param array $args Unused parameter.
161 * @param stdClass $response Plugin update response.
162 */
163 public function in_plugin_update_message( $args, $response ) {
164 $this->new_version = $response->new_version;
165 $this->upgrade_notice = $this->get_upgrade_notice( $response->new_version );
166
167 echo apply_filters( 'propertyhive_in_plugin_update_message', $this->upgrade_notice ? '<br><span style="color:#900">' . wp_kses_post( $this->upgrade_notice ) . '</span>' : '' ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
168 }
169
170 /**
171 * Get the upgrade notice from WordPress.org.
172 *
173 * @param string $version Property Hive new version.
174 * @return string
175 */
176 protected function get_upgrade_notice( $new_version ) {
177 $transient_name = 'ph_upgrade_notice_' . $new_version;
178 $upgrade_notice = get_transient( $transient_name );
179
180 if ( false === $upgrade_notice ) {
181 $response = wp_safe_remote_get( 'https://plugins.svn.wordpress.org/propertyhive/trunk/README.txt' );
182
183 if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
184 $upgrade_notice = $this->parse_update_notice( $response['body'], $new_version );
185 set_transient( $transient_name, $upgrade_notice, DAY_IN_SECONDS );
186 }
187 }
188 return $upgrade_notice;
189 }
190
191 /**
192 * Parse update notice from readme file.
193 *
194 * @param string $content Property Hive readme file content.
195 * @param string $new_version Property Hive new version.
196 * @return string
197 */
198 private function parse_update_notice( $content, $new_version ) {
199
200 $upgrade_notice = '';
201
202 $notices = explode("== Upgrade Notice ==", $content, 2);
203
204 if ( count($notices) < 2 ) { return ''; }
205
206 $notices = (array) preg_split( '~[\r\n]+~', trim( $notices[1] ) );
207
208 for ( $i = 0; $i < count($notices); ++$i )
209 {
210 $version = trim(str_replace("=", "", $notices[$i]));
211 if ( version_compare( $version, PH_VERSION, '>' ) )
212 {
213 $upgrade_notice .= '<br><strong>- ';
214 $upgrade_notice .= preg_replace( '~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $notices[$i+1] );
215 $upgrade_notice .= '</strong>';
216 }
217
218 ++$i;
219 }
220
221 return wp_kses_post( $upgrade_notice );
222 }
223 }
224 new PH_Plugin_Updates();