ad-health-notices.php
1 year ago
class-ad-network-ad-unit.php
4 months ago
class-ad-network.php
1 year ago
class-licenses.php
3 months ago
class-notices.php
4 months ago
class-overview-widgets.php
1 year ago
class-plugins-screen-updates.php
2 months ago
notices.php
4 months ago
class-plugins-screen-updates.php
139 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName |
| 2 | /** |
| 3 | * Manages Advanced Ads plugin updating notifications on the Plugins screen. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Class Advanced_Ads_Plugins_Screen_Updates |
| 15 | */ |
| 16 | class Advanced_Ads_Plugins_Screen_Updates { |
| 17 | |
| 18 | /** |
| 19 | * The upgrade notice shown inline. |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $upgrade_notice = ''; |
| 24 | |
| 25 | /** |
| 26 | * The current version of the plugin. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $current_version = ''; |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | */ |
| 35 | public function __construct() { |
| 36 | $this->current_version = defined( 'ADVADS_VERSION' ) ? ADVADS_VERSION : '0.0.0'; |
| 37 | |
| 38 | add_action( "in_plugin_update_message-advanced-ads/advanced-ads.php", array( $this, 'in_plugin_update_message' ), 20, 2 ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Show plugin changes on the plugins screen. |
| 43 | * |
| 44 | * @param array $args Unused parameter. |
| 45 | * @param stdClass $response Plugin update response. |
| 46 | */ |
| 47 | public function in_plugin_update_message( $args, $response ) { |
| 48 | $new_version = $response->new_version; |
| 49 | |
| 50 | if ( version_compare( $this->current_version, $new_version, '>=' ) ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | $this->upgrade_notice = $this->get_upgrade_notice( $new_version ); |
| 55 | |
| 56 | if ( empty( $this->upgrade_notice ) ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | echo '</p>'; |
| 61 | ?> |
| 62 | <div id="advads-update-notice-<?php echo esc_attr( $new_version ); ?>" style="display: flex; align-items: flex-start; padding: 1em 0; margin-top: .5em; border-top: 1px solid #dba617"> |
| 63 | <span class="dashicons dashicons-info" style="color: #ffba00; margin-right: 10px; flex-shrink: 0;"></span> |
| 64 | <div> |
| 65 | <strong style="display: block; margin-bottom: 4px;"><?php esc_html_e( 'Heads up!', 'advanced-ads' ); ?></strong> |
| 66 | <div style="line-height: 1.5; color: #3c434a;"> |
| 67 | <?php echo wp_kses_post( $this->upgrade_notice ); ?> |
| 68 | </div> |
| 69 | </div> |
| 70 | </div> |
| 71 | <?php |
| 72 | |
| 73 | echo '<p class="dummy" style="display:none;">'; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get the upgrade notice from WordPress.org. |
| 78 | * |
| 79 | * @param string $version The version to check. |
| 80 | * @return string |
| 81 | */ |
| 82 | protected function get_upgrade_notice( $version ) { |
| 83 | $transient_name = 'advads_upgrade_notice_' . $version; |
| 84 | $upgrade_notice = get_transient( $transient_name ); |
| 85 | |
| 86 | if ( false === $upgrade_notice ) { |
| 87 | $response = wp_safe_remote_get( 'https://plugins.svn.wordpress.org/advanced-ads/trunk/readme.txt' ); |
| 88 | |
| 89 | if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) { |
| 90 | $upgrade_notice = $this->parse_update_notice( $response['body'], $version ); |
| 91 | set_transient( $transient_name, $upgrade_notice, DAY_IN_SECONDS ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return $upgrade_notice; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Parse update notice from readme file. |
| 100 | * |
| 101 | * @param string $content Readme file content. |
| 102 | * @param string $new_version New version. |
| 103 | * @return string |
| 104 | */ |
| 105 | private function parse_update_notice( $content, $new_version ) { |
| 106 | $version_parts = explode( '.', $new_version ); |
| 107 | $check_for_notices = array( |
| 108 | $version_parts[0] . '.0', // Major. |
| 109 | $version_parts[0] . '.0.0', // Major. |
| 110 | $version_parts[0] . '.' . $version_parts[1], // Minor. |
| 111 | $version_parts[0] . '.' . $version_parts[1] . '.' . $version_parts[2], // Patch. |
| 112 | ); |
| 113 | |
| 114 | $notice_regexp = '~==\s*Upgrade Notice\s*==\s*=\s*(.*)\s*=(.*)(?===|$)~Uis'; |
| 115 | $upgrade_notice = ''; |
| 116 | |
| 117 | foreach ( $check_for_notices as $check_version ) { |
| 118 | if ( version_compare( $this->current_version, $check_version, '>=' ) ) { |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | $matches = null; |
| 123 | if ( preg_match( $notice_regexp, $content, $matches ) ) { |
| 124 | if ( version_compare( trim( $matches[1] ), $check_version, '=' ) ) { |
| 125 | $notices = (array) preg_split( '~[\r\n]+~', trim( $matches[2] ) ); |
| 126 | |
| 127 | foreach ( $notices as $line ) { |
| 128 | $upgrade_notice .= preg_replace( '~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $line ) . ' '; |
| 129 | } |
| 130 | |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return trim( $upgrade_notice ); |
| 137 | } |
| 138 | } |
| 139 |