1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Libraries\Plugin_Update_Warning; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Templater\Templater; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This class is responsible for handling custom sidebars. |
9
|
|
|
*/ |
10
|
|
|
class Plugin_Update_Warning { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Singleton implementation. |
14
|
|
|
* |
15
|
|
|
* @return Plugin_Update_Warning |
16
|
|
|
*/ |
17
|
|
|
public static function instance() { |
18
|
|
|
static $instance; |
19
|
|
|
if ( ! is_a( $instance, get_class() ) ) { |
20
|
|
|
$instance = new self(); |
21
|
|
|
add_action( 'admin_init', array( $instance, 'setup' ) ); |
22
|
|
|
} |
23
|
|
|
return $instance; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Register actions, filters, etc... |
28
|
|
|
*/ |
29
|
|
|
public function setup() { |
30
|
|
|
global $pagenow; |
31
|
|
|
|
32
|
|
|
if ( !in_array( $pagenow, array( 'plugins.php', 'update-core.php' ) ) ) { |
|
|
|
|
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ( !$this->has_major_update() ) { |
|
|
|
|
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
41
|
|
|
add_action( 'admin_print_footer_scripts', array( $this, 'print_json_data' ), 9 ); |
42
|
|
|
|
43
|
|
|
ob_start(); |
44
|
|
|
$this->template(); |
45
|
|
|
$template = ob_get_clean(); |
46
|
|
|
Templater::add_template( 'plugin-update-warning', $template ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function has_major_update() { |
50
|
|
|
$all_updates = get_plugin_updates(); |
51
|
|
|
$carbon_fields_data = isset( $all_updates[\Carbon_Fields\RELATIVE_PLUGIN_FILE] ) ? $all_updates[\Carbon_Fields\RELATIVE_PLUGIN_FILE] : null; |
|
|
|
|
52
|
|
|
if ( !$carbon_fields_data ) { |
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$plugin_data = (object) _get_plugin_data_markup_translate( \Carbon_Fields\RELATIVE_PLUGIN_FILE, (array) $carbon_fields_data, false, true ); |
57
|
|
|
$current_version = explode( '.', $plugin_data->Version ); |
58
|
|
|
$update_version = explode( '.', $plugin_data->update->new_version ); |
59
|
|
|
return ( intval( $current_version[0] ) < intval( $update_version[0] ) ); // compare only MAJOR part of SemVer |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the JSON data. |
64
|
|
|
*/ |
65
|
|
|
protected function get_json_data() { |
66
|
|
|
return array( |
67
|
|
|
'plugin_path' => \Carbon_Fields\RELATIVE_PLUGIN_FILE, |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create the warning template |
73
|
|
|
*/ |
74
|
|
|
protected function template() { |
75
|
|
|
?> |
76
|
|
|
<div class="update-message notice inline notice-error notice-alt"> |
77
|
|
|
<p><?php echo nl2br( sprintf( __( 'The new version of Carbon Fields is a major update. Please make sure you have a full backup before updating and test any add-ons or custom functionality.' . "\n" . 'Developers should review the upgrade guide on %1$s.', 'carbon-fields' ), '<a href="https://carbonfields.net/" target="_blank">carbonfields.net</a>' ) ); ?></p> |
|
|
|
|
78
|
|
|
</div> |
79
|
|
|
<?php |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Print the JSON data script. |
84
|
|
|
*/ |
85
|
|
|
public function print_json_data() { |
86
|
|
|
?> |
87
|
|
|
<script type="text/javascript"> |
88
|
|
|
<!--//--><![CDATA[//><!-- |
89
|
|
|
var carbonPluginUpdateWarningData = <?php echo wp_json_encode( $this->get_json_data() ); ?>; |
90
|
|
|
//--><!]]> |
91
|
|
|
</script> |
92
|
|
|
<?php |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Enqueue assets |
97
|
|
|
*/ |
98
|
|
|
public function enqueue_scripts() { |
99
|
|
|
wp_enqueue_style( 'carbon-plugin-update-warning', \Carbon_Fields\URL . '/core/Libraries/Plugin_Update_Warning/assets/css/app.css', array(), \Carbon_Fields\VERSION ); |
100
|
|
|
wp_enqueue_script( 'carbon-plugin-update-warning', \Carbon_Fields\URL . '/core/Libraries/Plugin_Update_Warning/assets/js/app.js', array( 'jquery', 'backbone' ), \Carbon_Fields\VERSION ); |
101
|
|
|
wp_localize_script( 'carbon-plugin-update-warning', 'crbPluginUpdateWarningL10n', |
102
|
|
|
array( |
|
|
|
|
103
|
|
|
|
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|