| 1 |
<?php |
| 2 |
namespace FPFramework\Admin\Includes; |
| 3 |
|
| 4 |
if (!defined('ABSPATH')) |
| 5 |
{ |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
class UpdateNotice |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Plugin alias for which we try to show an update notice. |
| 13 |
* |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
private $plugin_alias; |
| 17 |
|
| 18 |
/** |
| 19 |
* Plugin name for which we try to show an update notice. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $plugin_name; |
| 24 |
|
| 25 |
/** |
| 26 |
* Plugin version for which we try to show an update notice. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
private $plugin_version; |
| 31 |
|
| 32 |
/** |
| 33 |
* Holds the pages where the Update notice can run. |
| 34 |
* |
| 35 |
* @var array |
| 36 |
*/ |
| 37 |
private $valid_pages; |
| 38 |
|
| 39 |
public function __construct($plugin_alias = null, $plugin_name = null, $plugin_version = '', $valid_pages = []) |
| 40 |
{ |
| 41 |
$this->plugin_alias = $plugin_alias; |
| 42 |
$this->plugin_name = $plugin_name; |
| 43 |
$this->plugin_version = $plugin_version; |
| 44 |
$this->valid_pages = $valid_pages; |
| 45 |
} |
| 46 |
|
| 47 |
public function init() |
| 48 |
{ |
| 49 |
$this->registerAjax(); |
| 50 |
|
| 51 |
if (!$this->canRun()) |
| 52 |
{ |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
$this->loadMedia(); |
| 57 |
} |
| 58 |
|
| 59 |
private function canRun() |
| 60 |
{ |
| 61 |
if (!$this->plugin_alias && !$this->plugin_name && !$this->plugin_version && !$this->valid_pages) |
| 62 |
{ |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
global $pagenow; |
| 67 |
if (!$pagenow || $pagenow != 'admin.php') |
| 68 |
{ |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
$page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : ''; |
| 74 |
|
| 75 |
if (!in_array($page, $this->valid_pages)) |
| 76 |
{ |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
private function registerAjax() |
| 84 |
{ |
| 85 |
add_action('wp_ajax_fpf_show_update_notice', [$this, 'fpf_show_update_notice']); |
| 86 |
} |
| 87 |
|
| 88 |
public function fpf_show_update_notice() |
| 89 |
{ |
| 90 |
$url = FPF_GET_LICENSE_VERSION_URL . $this->plugin_alias; |
| 91 |
|
| 92 |
$response = wp_remote_get($url); |
| 93 |
|
| 94 |
if (!is_array($response)) |
| 95 |
{ |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
$response_decoded = null; |
| 100 |
|
| 101 |
try |
| 102 |
{ |
| 103 |
$response_decoded = json_decode( $response['body'] ); |
| 104 |
} |
| 105 |
catch ( Exception $ex ) |
| 106 |
{ |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
if (!isset($response_decoded->version)) |
| 111 |
{ |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
$new_version = $response_decoded->version; |
| 116 |
|
| 117 |
$installed_version = $this->plugin_version; |
| 118 |
|
| 119 |
if (!version_compare($installed_version, $new_version, '<')) |
| 120 |
{ |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
// load update notice |
| 125 |
$notice = fpframework()->renderer->render('admin/ui/update_notice', [ |
| 126 |
'plugin_name' => $this->plugin_name, |
| 127 |
'plugin_alias' => $this->plugin_alias, |
| 128 |
'current_version' => $this->plugin_version, |
| 129 |
'version' => $new_version |
| 130 |
], true); |
| 131 |
|
| 132 |
echo json_encode([ |
| 133 |
'html' => $notice |
| 134 |
]); |
| 135 |
wp_die(); |
| 136 |
} |
| 137 |
|
| 138 |
private function loadMedia() |
| 139 |
{ |
| 140 |
// load update notice css |
| 141 |
wp_register_style( |
| 142 |
'fpf-update-notice', |
| 143 |
FPF_MEDIA_URL . 'admin/css/fpf_update_notice.css', |
| 144 |
[], |
| 145 |
FPF_VERSION, |
| 146 |
false |
| 147 |
); |
| 148 |
wp_enqueue_style( 'fpf-update-notice' ); |
| 149 |
|
| 150 |
// load update notice js |
| 151 |
wp_register_script( |
| 152 |
'fpf-update-notice', |
| 153 |
FPF_MEDIA_URL . 'admin/js/fpf_update_notice.js', |
| 154 |
[], |
| 155 |
FPF_VERSION, |
| 156 |
false |
| 157 |
); |
| 158 |
wp_enqueue_script( 'fpf-update-notice' ); |
| 159 |
} |
| 160 |
} |