| 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 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- Legacy public global class PH_Plugin_Updates; preserving the existing PH_* class name is required for plugin and extension compatibility. |
| 18 |
class PH_Plugin_Updates { |
| 19 |
|
| 20 |
/** |
| 21 |
* The upgrade notice shown inline. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $upgrade_notice = ''; |
| 26 |
|
| 27 |
/** |
| 28 |
* Constructor. |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
add_action( 'in_plugin_update_message-propertyhive/propertyhive.php', array( $this, 'in_plugin_update_message' ), 10, 2 ); |
| 32 |
|
| 33 |
if ( is_network_admin() || ! is_multisite() ) |
| 34 |
{ |
| 35 |
$license_type = PH()->license->get_license_type(); |
| 36 |
|
| 37 |
$valid_license = false; |
| 38 |
|
| 39 |
if ( $license_type == 'old' ) |
| 40 |
{ |
| 41 |
$license = PH()->license->get_current_license(); |
| 42 |
|
| 43 |
if ( is_array($license) && empty($license) && get_option( 'propertyhive_license_key', '' ) != '' ) |
| 44 |
{ |
| 45 |
|
| 46 |
} |
| 47 |
elseif ( isset($license['active']) && $license['active'] != '1' ) |
| 48 |
{ |
| 49 |
|
| 50 |
} |
| 51 |
else |
| 52 |
{ |
| 53 |
if ( isset($license['expires_at']) && $license['expires_at'] != '' ) |
| 54 |
{ |
| 55 |
if ( strtotime($license['expires_at']) <= time() ) |
| 56 |
{ |
| 57 |
// Expired |
| 58 |
|
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
// Valid |
| 63 |
$valid_license = true; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
elseif ( $license_type == 'pro' ) |
| 69 |
{ |
| 70 |
// get new license information |
| 71 |
if ( get_option('propertyhive_pro_license_key', '') != '' ) |
| 72 |
{ |
| 73 |
if ( PH()->license->is_valid_pro_license_key() ) |
| 74 |
{ |
| 75 |
$valid_license = true; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if ( !$valid_license ) |
| 81 |
{ |
| 82 |
$add_ons = ''; |
| 83 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only refresh of public add-on metadata; this flag does not install, activate or change licensing. |
| 84 |
if ( false === ( $add_ons = get_transient( 'propertyhive_features' ) ) || isset($_GET['ph_force_get_features']) ) |
| 85 |
{ |
| 86 |
// It wasn't there, so regenerate the data and save the transient |
| 87 |
$response = wp_remote_get( |
| 88 |
'https://wp-property-hive.com/add-ons-json.php', |
| 89 |
array( |
| 90 |
'timeout' => 10 |
| 91 |
) |
| 92 |
); |
| 93 |
|
| 94 |
if ( !is_wp_error( $response ) && is_array( $response ) ) |
| 95 |
{ |
| 96 |
$body = $response['body']; // use the content |
| 97 |
|
| 98 |
$add_ons = json_decode($body, TRUE); |
| 99 |
|
| 100 |
if ( $add_ons !== FALSE && is_array($add_ons) && !empty($add_ons) ) |
| 101 |
{ |
| 102 |
set_transient( 'propertyhive_features', $add_ons, DAY_IN_SECONDS ); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if ($add_ons !== FALSE && !empty($add_ons)) |
| 108 |
{ |
| 109 |
foreach ($add_ons as $add_on) |
| 110 |
{ |
| 111 |
$url = trim($add_on['url'], '/'); |
| 112 |
|
| 113 |
if ( |
| 114 |
isset($add_on['wordpress_plugin_file']) && $add_on['wordpress_plugin_file'] != '' && $add_on['wordpress_plugin_file'] != false && |
| 115 |
isset($add_on['wordpress_version_option_name']) && $add_on['wordpress_version_option_name'] != '' && $add_on['wordpress_version_option_name'] != false |
| 116 |
) |
| 117 |
{ |
| 118 |
$plugin_file = $add_on['wordpress_plugin_file']; |
| 119 |
|
| 120 |
if ( is_plugin_active($plugin_file) ) |
| 121 |
{ |
| 122 |
$current_version = get_option($add_on['wordpress_version_option_name'], ''); |
| 123 |
|
| 124 |
if ( $current_version != '' ) |
| 125 |
{ |
| 126 |
// check if add on update available |
| 127 |
$new_version = $add_on['version']; |
| 128 |
|
| 129 |
if ( version_compare($new_version, $current_version) == 1 ) |
| 130 |
{ |
| 131 |
add_action( 'after_plugin_row_' . $plugin_file, array( $this, 'test' ), 10, 3 ); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public function test( $plugin_file, $plugin_data, $status ) |
| 143 |
{ |
| 144 |
if ( is_network_admin() ) { |
| 145 |
$active_class = is_plugin_active_for_network( $plugin_file ) ? ' active' : ''; |
| 146 |
} else { |
| 147 |
$active_class = is_plugin_active( $plugin_file ) ? ' active' : ''; |
| 148 |
} |
| 149 |
|
| 150 |
echo '<tr class="plugin-update-tr' . esc_attr($active_class) . '"> |
| 151 |
<td colspan="3" class="plugin-update colspanchange"> |
| 152 |
<div class="update-message notice inline notice-warning notice-alt"> |
| 153 |
<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> |
| 154 |
</div> |
| 155 |
</td> |
| 156 |
</tr>'; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Show plugin changes on the plugins screen. |
| 161 |
* |
| 162 |
* @param array $args Unused parameter. |
| 163 |
* @param stdClass $response Plugin update response. |
| 164 |
*/ |
| 165 |
public function in_plugin_update_message( $args, $response ) { |
| 166 |
$this->new_version = $response->new_version; |
| 167 |
$this->upgrade_notice = $this->get_upgrade_notice( $response->new_version ); |
| 168 |
|
| 169 |
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.Security.EscapeOutput.OutputNotEscaped -- The remote notice is sanitized with wp_kses_post before this trusted PHP extension HTML filter. |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get the upgrade notice from WordPress.org. |
| 174 |
* |
| 175 |
* @param string $version Property Hive new version. |
| 176 |
* @return string |
| 177 |
*/ |
| 178 |
protected function get_upgrade_notice( $new_version ) { |
| 179 |
$transient_name = 'ph_upgrade_notice_' . $new_version; |
| 180 |
$upgrade_notice = get_transient( $transient_name ); |
| 181 |
|
| 182 |
if ( false === $upgrade_notice ) { |
| 183 |
$response = wp_safe_remote_get( 'https://plugins.svn.wordpress.org/propertyhive/trunk/README.txt' ); |
| 184 |
|
| 185 |
if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) { |
| 186 |
$upgrade_notice = $this->parse_update_notice( $response['body'], $new_version ); |
| 187 |
set_transient( $transient_name, $upgrade_notice, DAY_IN_SECONDS ); |
| 188 |
} |
| 189 |
} |
| 190 |
return $upgrade_notice; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Parse update notice from readme file. |
| 195 |
* |
| 196 |
* @param string $content Property Hive readme file content. |
| 197 |
* @param string $new_version Property Hive new version. |
| 198 |
* @return string |
| 199 |
*/ |
| 200 |
private function parse_update_notice( $content, $new_version ) { |
| 201 |
|
| 202 |
$upgrade_notice = ''; |
| 203 |
|
| 204 |
$notices = explode("== Upgrade Notice ==", $content, 2); |
| 205 |
|
| 206 |
if ( count($notices) < 2 ) { return ''; } |
| 207 |
|
| 208 |
$notices = (array) preg_split( '~[\r\n]+~', trim( $notices[1] ) ); |
| 209 |
|
| 210 |
for ( $i = 0; $i < count($notices); ++$i ) |
| 211 |
{ |
| 212 |
$version = trim(str_replace("=", "", $notices[$i])); |
| 213 |
if ( version_compare( $version, PH_VERSION, '>' ) ) |
| 214 |
{ |
| 215 |
$upgrade_notice .= '<br><strong>- '; |
| 216 |
$upgrade_notice .= preg_replace( '~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $notices[$i+1] ); |
| 217 |
$upgrade_notice .= '</strong>'; |
| 218 |
} |
| 219 |
|
| 220 |
++$i; |
| 221 |
} |
| 222 |
|
| 223 |
return wp_kses_post( $upgrade_notice ); |
| 224 |
} |
| 225 |
} |
| 226 |
new PH_Plugin_Updates(); |