actions.php
697 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Add-ons Actions |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Admin/Add-ons/Actions |
| 7 | * @copyright Copyright (c) 2019, GiveWP |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 2.5.0 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Ajax addon upload handler |
| 19 | * |
| 20 | * Note: only for internal use |
| 21 | * |
| 22 | * @since 2.5.0 |
| 23 | */ |
| 24 | function give_upload_addon_handler() { |
| 25 | /* @var WP_Filesystem_Direct $wp_filesystem */ |
| 26 | global $wp_filesystem; |
| 27 | |
| 28 | check_admin_referer( 'give-upload-addon' ); |
| 29 | |
| 30 | // Remove version from file name. |
| 31 | $filename = preg_replace( [ '/\(\d\).zip/', '/(.\d).*[\(\d\)]/' ], '', $_FILES['file']['name'] ); |
| 32 | $filename = basename( trim( $filename ), '.zip' ); |
| 33 | |
| 34 | // Bailout if user does not has permission. |
| 35 | if ( ! current_user_can( 'upload_plugins' ) ) { |
| 36 | wp_send_json_error( [ 'errorMsg' => __( 'The current user does not have permission to upload plugins on this site.', 'give' ) ] ); |
| 37 | } |
| 38 | |
| 39 | $access_type = get_filesystem_method(); |
| 40 | |
| 41 | if ( 'direct' !== $access_type ) { |
| 42 | wp_send_json_error( |
| 43 | [ |
| 44 | 'errorMsg' => sprintf( |
| 45 | __( 'In order to upload add-ons here, GiveWP needs direct access to the file system. Please <a href="%1$s" target="_blank">visit the main plugin page</a> to manually upload the add-on.', 'give' ), |
| 46 | admin_url( 'plugin-install.php?tab=upload' ) |
| 47 | ), |
| 48 | ] |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | $file_type = wp_check_filetype( $_FILES['file']['name'], [ 'zip' => 'application/zip' ] ); |
| 53 | |
| 54 | if ( empty( $file_type['ext'] ) ) { |
| 55 | wp_send_json_error( [ 'errorMsg' => __( 'Uploaded add-ons must be (zipped) ZIP files. Upload a valid add-on ZIP.', 'give' ) ] ); |
| 56 | } |
| 57 | |
| 58 | $give_addons_list = give_get_plugins(); |
| 59 | $is_addon_installed = []; |
| 60 | |
| 61 | if ( ! empty( $give_addons_list ) ) { |
| 62 | foreach ( $give_addons_list as $addon => $give_addon ) { |
| 63 | if ( false !== stripos( $addon, $filename ) ) { |
| 64 | $is_addon_installed = $give_addon; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Bailout if addon already installed |
| 70 | if ( ! empty( $is_addon_installed ) ) { |
| 71 | wp_send_json_error( |
| 72 | [ |
| 73 | 'errorMsg' => __( 'This add-on is already installed.', 'give' ), |
| 74 | 'pluginInfo' => $is_addon_installed, |
| 75 | ] |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | $upload_status = wp_handle_upload( $_FILES['file'], [ 'test_form' => false ] ); |
| 80 | |
| 81 | // Bailout if has any upload error |
| 82 | if ( empty( $upload_status['file'] ) ) { |
| 83 | wp_send_json_error( $upload_status ); |
| 84 | } |
| 85 | |
| 86 | // @todo: check how WordPress verify plugin files before uploading to plugin directory |
| 87 | |
| 88 | /* you can safely run request_filesystem_credentials() without any issues and don't need to worry about passing in a URL */ |
| 89 | $creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, [] ); |
| 90 | |
| 91 | /* initialize the API */ |
| 92 | if ( ! WP_Filesystem( $creds ) ) { |
| 93 | /* any problems and we exit */ |
| 94 | wp_send_json_error( |
| 95 | [ |
| 96 | 'errorMsg' => __( 'The file system did not load correctly. This is usually a permissions issue on your server, and not something that GiveWP has control over. Try uploading the ZIP like a regular plugin.', 'give' ), |
| 97 | ] |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | $unzip_status = unzip_file( $upload_status['file'], $wp_filesystem->wp_plugins_dir() ); |
| 102 | |
| 103 | // Remove file. |
| 104 | @unlink( $upload_status['file'] ); |
| 105 | |
| 106 | // Bailout if not able to unzip file successfully |
| 107 | if ( is_wp_error( $unzip_status ) ) { |
| 108 | wp_send_json_error( |
| 109 | [ |
| 110 | 'errorMsg' => $unzip_status, |
| 111 | ] |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | // Delete cache and get current installed addon plugin path. |
| 116 | wp_clean_plugins_cache( true ); |
| 117 | |
| 118 | $give_addons_list = give_get_plugins(); |
| 119 | $installed_addon = []; |
| 120 | |
| 121 | if ( ! empty( $give_addons_list ) ) { |
| 122 | foreach ( $give_addons_list as $addon => $give_addon ) { |
| 123 | if ( false !== stripos( $addon, $filename ) ) { |
| 124 | $installed_addon = $give_addon; |
| 125 | $installed_addon['path'] = $addon; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | wp_send_json_success( |
| 131 | [ |
| 132 | 'pluginPath' => $installed_addon['path'], |
| 133 | 'pluginName' => $installed_addon['Name'], |
| 134 | 'nonce' => wp_create_nonce( "give_activate-{$installed_addon['path']}" ), |
| 135 | 'licenseSectionHtml' => Give_License::render_licenses_list(), |
| 136 | ] |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | add_action( 'wp_ajax_give_upload_addon', 'give_upload_addon_handler' ); |
| 141 | |
| 142 | /** |
| 143 | * Ajax license inquiry handler |
| 144 | * |
| 145 | * Note: only for internal use |
| 146 | * |
| 147 | * @since 2.5.0 |
| 148 | */ |
| 149 | function give_get_license_info_handler() { |
| 150 | check_admin_referer( 'give-license-activator-nonce' ); |
| 151 | |
| 152 | // check user permission. |
| 153 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 154 | give_die(); |
| 155 | } |
| 156 | |
| 157 | $license_key = ! empty( $_POST['license'] ) ? give_clean( $_POST['license'] ) : ''; |
| 158 | $is_activating_single_license = ! empty( $_POST['single'] ) ? absint( $_POST['single'] ) : ''; |
| 159 | $is_reactivating_license = ! empty( $_POST['reactivate'] ) ? absint( $_POST['reactivate'] ) : ''; |
| 160 | $plugin_slug = $is_activating_single_license ? give_clean( $_POST['addon'] ) : ''; |
| 161 | $licenses = get_option( 'give_licenses', [] ); |
| 162 | |
| 163 | if ( ! $license_key ) { |
| 164 | wp_send_json_error( |
| 165 | [ |
| 166 | 'errorMsg' => __( 'You entered an invalid key. Confirm your license key on your GiveWP dashboard and try again.', 'give' ), |
| 167 | ] |
| 168 | ); |
| 169 | |
| 170 | } elseif ( |
| 171 | ! $is_reactivating_license |
| 172 | && array_key_exists( $license_key, $licenses ) |
| 173 | ) { |
| 174 | // If admin already activated license but did not install add-on then send license info show notice to admin with download link. |
| 175 | $license = $licenses[ $license_key ]; |
| 176 | if ( empty( $license['is_all_access_pass'] ) ) { |
| 177 | $plugin_data = Give_License::get_plugin_by_slug( $license['plugin_slug'] ); |
| 178 | |
| 179 | // Plugin license activated but does not install, sent notice which allow admin to download add-on. |
| 180 | if ( empty( $plugin_data ) ) { |
| 181 | wp_send_json_success( $license ); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | wp_send_json_error( |
| 186 | [ |
| 187 | 'errorMsg' => __( 'This license key is already in use on this website.', 'give' ), |
| 188 | ] |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | // Check license. |
| 193 | $check_license_res = Give_License::request_license_api( |
| 194 | [ |
| 195 | 'edd_action' => 'check_license', |
| 196 | 'license' => $license_key, |
| 197 | ], |
| 198 | true |
| 199 | ); |
| 200 | |
| 201 | // Make sure there are no errors. |
| 202 | if ( is_wp_error( $check_license_res ) ) { |
| 203 | wp_send_json_error( |
| 204 | [ |
| 205 | 'errorMsg' => $check_license_res->get_error_message(), |
| 206 | ] |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | // Check if license valid or not. |
| 211 | if ( ! $check_license_res['success'] ) { |
| 212 | wp_send_json_error( |
| 213 | [ |
| 214 | 'errorMsg' => sprintf( |
| 215 | __( 'The license failed to activate, due to a status of <code>%2$s</code>. Check the logs at Donations > Tools > Logs for more detail, and <a href="%1$s" target="_blank">reach out to the Customer Success team.</a>', 'give' ), |
| 216 | Give_License::get_account_url(), |
| 217 | $check_license_res['license'] |
| 218 | ), |
| 219 | ] |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | if ( |
| 224 | $is_activating_single_license |
| 225 | && ! empty( $check_license_res['plugin_slug'] ) |
| 226 | && $plugin_slug !== $check_license_res['plugin_slug'] |
| 227 | ) { |
| 228 | wp_send_json_error( |
| 229 | [ |
| 230 | 'errorMsg' => sprintf( |
| 231 | __( 'This license key does not belong to this add-on. <a href="%1$s" target="_blank">Reach out to the Customer Success team</a> if you continue to have issues.', 'give' ), |
| 232 | Give_License::get_account_url() |
| 233 | ), |
| 234 | ] |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | // Activate license. |
| 239 | $activate_license_res = Give_License::request_license_api( |
| 240 | [ |
| 241 | 'edd_action' => 'activate_license', |
| 242 | 'item_name' => $check_license_res['item_name'], |
| 243 | 'license' => $license_key, |
| 244 | ], |
| 245 | true |
| 246 | ); |
| 247 | |
| 248 | if ( is_wp_error( $activate_license_res ) ) { |
| 249 | wp_send_json_error( |
| 250 | [ |
| 251 | 'errorMsg' => $check_license_res->get_error_message(), |
| 252 | ] |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | // Return error if license activation is not success and admin is not reactivating add-on. |
| 257 | if ( ! $is_reactivating_license && ! $activate_license_res['success'] ) { |
| 258 | |
| 259 | $response['errorMsg'] = sprintf( |
| 260 | __( 'The license failed to activate, due to a status of <code>%2$s</code>. Check the logs at Donations > Tools > Logs for more detail, and <a href="%1$s" target="_blank">reach out to the Customer Success team.</a>', 'give' ), |
| 261 | Give_License::get_account_url(), |
| 262 | $check_license_res['license'] |
| 263 | ); |
| 264 | |
| 265 | wp_send_json_error( $response ); |
| 266 | } |
| 267 | |
| 268 | $check_license_res['license'] = $activate_license_res['license']; |
| 269 | $check_license_res['site_count'] = $activate_license_res['site_count']; |
| 270 | $check_license_res['activations_left'] = $activate_license_res['activations_left']; |
| 271 | |
| 272 | // Remove single license which is part of all access pass or already existed all access pass key because admin can only one active. |
| 273 | // @see https://github.com/impress-org/givewp/issues/4669 |
| 274 | if ( ! empty( $check_license_res['is_all_access_pass'] ) ) { |
| 275 | $addonSlugs = Give_License::getAddonSlugsFromAllAccessPassLicense( $check_license_res ); |
| 276 | foreach ( $licenses as $license_key => $data ) { |
| 277 | if ( in_array( $data['plugin_slug'], $addonSlugs, true ) || ! empty( $data['is_all_access_pass'] ) ) { |
| 278 | unset( $licenses[ $license_key ] ); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | $licenses[ $check_license_res['license_key'] ] = $check_license_res; |
| 284 | update_option( 'give_licenses', $licenses ); |
| 285 | |
| 286 | // Get license section HTML. |
| 287 | $response = $check_license_res; |
| 288 | $response['html'] = $is_activating_single_license && empty( $check_license_res['is_all_access_pass'] ) |
| 289 | ? Give_License::html_by_plugin( Give_License::get_plugin_by_slug( $check_license_res['plugin_slug'] ) ) |
| 290 | : Give_License::render_licenses_list(); |
| 291 | |
| 292 | // Return error if license activation is not success and admin is reactivating add-on. |
| 293 | if ( $is_reactivating_license && ! $activate_license_res['success'] ) { |
| 294 | |
| 295 | $response['errorMsg'] = sprintf( |
| 296 | __( 'The license failed to activate, due to a status of <code>%2$s</code>. Check the logs at Donations > Tools > Logs for more detail, and <a href="%1$s" target="_blank">reach out to the Customer Success team.</a>', 'give' ), |
| 297 | Give_License::get_account_url(), |
| 298 | $check_license_res['license'] |
| 299 | ); |
| 300 | |
| 301 | wp_send_json_error( $response ); |
| 302 | } |
| 303 | |
| 304 | // Tell WordPress to look for updates. |
| 305 | give_refresh_licenses(); |
| 306 | |
| 307 | wp_send_json_success( $response ); |
| 308 | } |
| 309 | |
| 310 | add_action( 'wp_ajax_give_get_license_info', 'give_get_license_info_handler' ); |
| 311 | |
| 312 | |
| 313 | /** |
| 314 | * Activate addon handler |
| 315 | * |
| 316 | * Note: only for internal use |
| 317 | * |
| 318 | * @since 2.5.0 |
| 319 | */ |
| 320 | function give_activate_addon_handler() { |
| 321 | $plugin_path = give_clean( $_POST['plugin'] ); |
| 322 | |
| 323 | check_admin_referer( "give_activate-{$plugin_path}" ); |
| 324 | |
| 325 | // check user permission. |
| 326 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 327 | give_die(); |
| 328 | } |
| 329 | |
| 330 | $status = activate_plugin( $plugin_path ); |
| 331 | |
| 332 | if ( is_wp_error( $status ) ) { |
| 333 | wp_send_json_error( [ 'errorMsg' => $status->get_error_message() ] ); |
| 334 | } |
| 335 | |
| 336 | // Tell WordPress to look for updates. |
| 337 | give_refresh_licenses(); |
| 338 | |
| 339 | wp_send_json_success( |
| 340 | [ |
| 341 | 'licenseSectionHtml' => Give_License::render_licenses_list(), |
| 342 | ] |
| 343 | ); |
| 344 | } |
| 345 | |
| 346 | add_action( 'wp_ajax_give_activate_addon', 'give_activate_addon_handler' ); |
| 347 | |
| 348 | |
| 349 | /** |
| 350 | * deactivate addon handler |
| 351 | * |
| 352 | * Note: only for internal use |
| 353 | * |
| 354 | * @since 2.5.0 |
| 355 | */ |
| 356 | function give_deactivate_license_handler() { |
| 357 | $license = give_clean( $_POST['license'] ); |
| 358 | $item_name = give_clean( $_POST['item_name'] ); |
| 359 | $plugin_dirname = give_clean( $_POST['plugin_dirname'] ); |
| 360 | |
| 361 | if ( ! $license || ! $item_name ) { |
| 362 | wp_send_json_error(); |
| 363 | } |
| 364 | |
| 365 | check_admin_referer( "give-deactivate-license-{$item_name}" ); |
| 366 | |
| 367 | // check user permission. |
| 368 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 369 | give_die(); |
| 370 | } |
| 371 | |
| 372 | $give_licenses = get_option( 'give_licenses', [] ); |
| 373 | |
| 374 | if ( empty( $give_licenses[ $license ] ) ) { |
| 375 | wp_send_json_error( |
| 376 | [ |
| 377 | 'errorMsg' => __( 'License is invalid, so it can\'t be deactivated.', 'give' ), |
| 378 | ] |
| 379 | ); |
| 380 | } |
| 381 | |
| 382 | /* @var array|WP_Error $response */ |
| 383 | $response = Give_License::request_license_api( |
| 384 | [ |
| 385 | 'edd_action' => 'deactivate_license', |
| 386 | 'license' => $license, |
| 387 | 'item_name' => $item_name, |
| 388 | ], |
| 389 | true |
| 390 | ); |
| 391 | |
| 392 | if ( is_wp_error( $response ) ) { |
| 393 | wp_send_json_error( |
| 394 | [ |
| 395 | 'errorMsg' => $response->get_error_message(), |
| 396 | 'response' => $license, |
| 397 | ] |
| 398 | ); |
| 399 | } |
| 400 | |
| 401 | $is_all_access_pass = $give_licenses[ $license ]['is_all_access_pass']; |
| 402 | |
| 403 | if ( ! empty( $give_licenses[ $license ] ) ) { |
| 404 | unset( $give_licenses[ $license ] ); |
| 405 | update_option( 'give_licenses', $give_licenses ); |
| 406 | } |
| 407 | |
| 408 | $response['html'] = $is_all_access_pass |
| 409 | ? Give_License::render_licenses_list() |
| 410 | : Give_License::html_by_plugin( Give_License::get_plugin_by_slug( $plugin_dirname ) ); |
| 411 | |
| 412 | $response['msg'] = __( 'You have successfully deactivated the license.', 'give' ); |
| 413 | |
| 414 | // Tell WordPress to look for updates. |
| 415 | give_refresh_licenses(); |
| 416 | |
| 417 | wp_send_json_success( $response ); |
| 418 | } |
| 419 | |
| 420 | add_action( 'wp_ajax_give_deactivate_license', 'give_deactivate_license_handler' ); |
| 421 | |
| 422 | |
| 423 | /** |
| 424 | * Refresh all addons licenses handler |
| 425 | * |
| 426 | * Note: only for internal use |
| 427 | * |
| 428 | * @since 2.5.0 |
| 429 | */ |
| 430 | function give_refresh_all_licenses_handler() { |
| 431 | check_admin_referer( 'give-refresh-all-licenses' ); |
| 432 | |
| 433 | // check user permission. |
| 434 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 435 | give_die(); |
| 436 | } |
| 437 | |
| 438 | $data = Give_License::refresh_license_status(); |
| 439 | |
| 440 | // Update date and reset counter. |
| 441 | if ( $data['compare'] === date( 'Ymd' ) && 5 <= $data['count'] ) { |
| 442 | wp_send_json_error(); |
| 443 | } |
| 444 | |
| 445 | // Update date and reset counter. |
| 446 | if ( $data['compare'] < date( 'Ymd' ) ) { |
| 447 | $data['compare'] = date( 'Ymd' ); |
| 448 | $data['count'] = 0; |
| 449 | } |
| 450 | |
| 451 | // Update time. |
| 452 | $data['time'] = time(); |
| 453 | |
| 454 | ++ $data['count']; |
| 455 | |
| 456 | update_option( 'give_licenses_refreshed_last_checked', $data, 'no' ); |
| 457 | |
| 458 | give_refresh_licenses(); |
| 459 | |
| 460 | $local_date = strtotime( get_date_from_gmt( date( 'Y-m-d H:i:s', $data['time'] ) ) ); |
| 461 | wp_send_json_success( |
| 462 | [ |
| 463 | 'html' => Give_License::render_licenses_list(), |
| 464 | 'refreshButton' => 5 <= $data['count'], |
| 465 | 'refreshStatus' => $data, |
| 466 | 'lastUpdateMsg' => sprintf( |
| 467 | __( 'Last refreshed on %1$s at %2$s', 'give' ), |
| 468 | date( give_date_format(), $local_date ), |
| 469 | date( 'g:i a', $local_date ) |
| 470 | ), |
| 471 | ] |
| 472 | ); |
| 473 | } |
| 474 | |
| 475 | add_action( 'wp_ajax_give_refresh_all_licenses', 'give_refresh_all_licenses_handler' ); |
| 476 | |
| 477 | |
| 478 | /** |
| 479 | * Updates information on the "View version x.x details" page with custom data. |
| 480 | * Note: only for internal use |
| 481 | * |
| 482 | * @param mixed $_data |
| 483 | * @param string $_action |
| 484 | * @param object $_args |
| 485 | * |
| 486 | * @return object $_data |
| 487 | * @since 2.5.0 |
| 488 | * @uses api_request() |
| 489 | */ |
| 490 | function give_plugins_api_filter( $_data, $_action = '', $_args = null ) { |
| 491 | // Exit. |
| 492 | if ( 'plugin_information' !== $_action ) { |
| 493 | return $_data; |
| 494 | } |
| 495 | |
| 496 | $plugin = Give_License::get_plugin_by_slug( $_args->slug ); |
| 497 | |
| 498 | if ( |
| 499 | ! $plugin |
| 500 | || 'add-on' !== $plugin['Type'] |
| 501 | || false === strpos( $_args->slug, 'give-' ) |
| 502 | ) { |
| 503 | return $_data; |
| 504 | } |
| 505 | |
| 506 | $plugin_data = get_site_transient( 'update_plugins' ); |
| 507 | |
| 508 | if ( ! $plugin_data ) { |
| 509 | give_refresh_licenses(); |
| 510 | } |
| 511 | |
| 512 | $plugin_data = ! empty( $plugin_data->response[ $plugin['Path'] ] ) |
| 513 | ? $plugin_data->response[ $plugin['Path'] ] |
| 514 | : []; |
| 515 | |
| 516 | if ( ! $plugin_data ) { |
| 517 | return $_data; |
| 518 | } |
| 519 | |
| 520 | $_data = $plugin_data; |
| 521 | |
| 522 | return $_data; |
| 523 | } |
| 524 | |
| 525 | add_filter( 'plugins_api', 'give_plugins_api_filter', 9999, 3 ); |
| 526 | |
| 527 | |
| 528 | /** |
| 529 | * Check add-ons updates when WordPress check plugin updates |
| 530 | * |
| 531 | * @since 2.5.0 |
| 532 | */ |
| 533 | add_filter( 'pre_set_site_transient_update_plugins', 'give_check_addon_updates', 999, 1 ); |
| 534 | |
| 535 | |
| 536 | /** |
| 537 | * Show plugin update notification on multi-site |
| 538 | * |
| 539 | * @param string $file |
| 540 | * @param array $plugin |
| 541 | * |
| 542 | * @since 2.5.0 |
| 543 | */ |
| 544 | function give_show_update_notification_on_multisite( $file, $plugin ) { |
| 545 | if ( is_network_admin() ) { |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | if ( ! current_user_can( 'update_plugins' ) ) { |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | if ( ! is_multisite() ) { |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | if ( |
| 558 | ! $plugin |
| 559 | || empty( $plugin['slug'] ) |
| 560 | || false === strpos( $plugin['slug'], 'give-' ) |
| 561 | ) { |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | $plugin_data = Give_License::get_plugin_by_slug( $plugin['slug'] ); |
| 566 | |
| 567 | // Only show notices for Give add-ons |
| 568 | if ( 'add-on' !== $plugin_data['Type'] ) { |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | // Do not print any message if updates does not exist. |
| 573 | $update_cache = get_site_transient( 'update_plugins' ); |
| 574 | |
| 575 | if ( ! isset( $update_cache->response[ $file ] ) ) { |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | if ( ! empty( $update_cache->response[ $plugin_data['Path'] ] ) && version_compare( $plugin_data['Version'], $plugin['new_version'], '<' ) ) { |
| 580 | printf( |
| 581 | '<tr class="plugin-update-tr %3$s" id="%1$s-update" data-slug="%1$s" data-plugin="%2$s">', |
| 582 | $plugin['slug'], |
| 583 | $file, |
| 584 | 'active' === $plugin_data['Status'] ? 'active' : 'inactive' |
| 585 | ); |
| 586 | |
| 587 | echo '<td colspan="3" class="plugin-update colspanchange">'; |
| 588 | echo '<div class="update-message notice inline notice-warning notice-alt"><p>'; |
| 589 | |
| 590 | $changelog_link = self_admin_url( "plugin-install.php?tab=plugin-information&plugin={$plugin['slug']}§ion=changelog&TB_iframe=true&width=772&height=299" ); |
| 591 | |
| 592 | if ( empty( $plugin['download_link'] ) ) { |
| 593 | printf( |
| 594 | __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'give' ), |
| 595 | esc_html( $plugin_data['Name'] ), |
| 596 | '<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">', |
| 597 | esc_html( $plugin['new_version'] ), |
| 598 | '</a>' |
| 599 | ); |
| 600 | } else { |
| 601 | printf( |
| 602 | __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s or %5$supdate now%6$s.', 'give' ), |
| 603 | esc_html( $plugin_data['Name'] ), |
| 604 | '<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">', |
| 605 | esc_html( $plugin['new_version'] ), |
| 606 | '</a>', |
| 607 | '<a target="_blank" class="update-link" href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $file, 'upgrade-plugin_' . $file ) ) . '">', |
| 608 | '</a>' |
| 609 | ); |
| 610 | } |
| 611 | |
| 612 | do_action( "in_plugin_update_message-{$file}", $plugin, $plugin ); |
| 613 | |
| 614 | echo '</p></div></td></tr>'; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | add_action( 'after_plugin_row', 'give_show_update_notification_on_multisite', 10, 2 ); |
| 619 | |
| 620 | /** |
| 621 | * Show plugin update notification on single site |
| 622 | * |
| 623 | * @param $file |
| 624 | * @param $plugin |
| 625 | * |
| 626 | * @since 2.5.0 |
| 627 | * @since 2.10.2 update condition to verify givewp addons |
| 628 | */ |
| 629 | function give_show_update_notification_on_single_site( $file, $plugin ) { |
| 630 | if ( ! current_user_can( 'update_plugins' ) || is_multisite() ) { |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | if ( |
| 635 | ! $plugin |
| 636 | || empty( $plugin['slug'] ) |
| 637 | || false === strpos( $plugin['slug'], 'give-' ) |
| 638 | ) { |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | $plugin_data = Give_License::get_plugin_by_slug( $plugin['slug'] ); |
| 643 | |
| 644 | // Only show notices for Give add-ons |
| 645 | if ( |
| 646 | ! $plugin_data |
| 647 | || 'add-on' !== $plugin_data['Type'] |
| 648 | || $plugin_data['License'] |
| 649 | ) { |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | // Do not print any message if updates does not exist. |
| 654 | $update_plugins = get_site_transient( 'update_plugins' ); |
| 655 | if ( ! isset( $update_plugins->response[ $file ] ) ) { |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | // Remove core update notice. |
| 660 | remove_action( "after_plugin_row_{$file}", 'wp_plugin_update_row' ); |
| 661 | |
| 662 | $update_notice_wrap = '<tr class="plugin-update-tr %3$s"><td colspan="3" class="colspanchange"><div class="update-message notice inline notice-warning notice-alt give-invalid-license"><p>%1$s %2$s</p></div></td></tr>'; |
| 663 | $changelog_link = self_admin_url( "plugin-install.php?tab=plugin-information&plugin={$plugin['slug']}§ion=changelog&TB_iframe=true&width=772&height=299" ); |
| 664 | |
| 665 | echo sprintf( |
| 666 | $update_notice_wrap, |
| 667 | sprintf( |
| 668 | __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'give' ), |
| 669 | esc_html( $plugin_data['Name'] ), |
| 670 | '<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">', |
| 671 | esc_html( $plugin['new_version'] ), |
| 672 | '</a>' |
| 673 | ), |
| 674 | sprintf( |
| 675 | 'Please <a href="%1$s" target="_blank">activate your license</a> to receive updates and support.', |
| 676 | esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ) ) |
| 677 | ), |
| 678 | 'active' === $plugin_data['Status'] ? 'active' : 'inactive' |
| 679 | ); |
| 680 | } |
| 681 | |
| 682 | add_action( 'after_plugin_row', 'give_show_update_notification_on_single_site', 1, 2 ); |
| 683 | |
| 684 | /** |
| 685 | * Look for Give core add-ons update when someone manually presses the "Check Again" button within WP's Dashboard > Updates |
| 686 | * |
| 687 | * @since 2.5.11 |
| 688 | */ |
| 689 | function give_refresh_license_on_force_check() { |
| 690 | if ( isset( $_GET['force-check'] ) ) { |
| 691 | give_refresh_licenses(); |
| 692 | } |
| 693 | } |
| 694 | add_action( 'load-update-core.php', 'give_refresh_license_on_force_check', 9 ); |
| 695 | |
| 696 | |
| 697 |