Completed
Push — milestone/2_0/react-ui ( 1bad2b...5067c7 )
by
unknown
03:05
created

Plugin_Update_Warning::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Libraries\Plugin_Update_Warning;
4
5
/**
6
 * This class is responsible for displaying a plugin update warning when a major version is released.
7
 */
8
class Plugin_Update_Warning {
9
10
	public static function instance() {
11
		static $instance;
12
		if ( ! is_a( $instance, get_class() ) ) {
13
			$instance = new self();
14
		}
15
		return $instance;
16
	}
17
18
	public static function boot() {
19
		add_action( 'admin_head', array( static::instance(), 'setup' ) );
20
	}
21
22
	public function setup() {
23
		global $pagenow;
24
25
		if ( ! in_array( $pagenow, array( 'plugins.php', 'update-core.php' ) ) ) {
26
			return;
27
		}
28
29
		if ( ! $this->has_major_update() ) {
30
			return;
31
		}
32
33
		add_action( 'admin_footer', array( $this, 'enqueue_scripts' ) );
34
35
		ob_start();
36
		$this->template();
37
		$template = ob_get_clean();
38
		add_action( 'admin_footer', function() use ( $template ) {
39
			?>
40
			<script type="text/html" id="crb-tmpl-plugin-update-warning">
41
				<?php echo apply_filters( 'carbon_fields_plugin_update_warning_template', $template ); ?>
1 ignored issue
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'apply_filters'
Loading history...
42
			</script>
43
			<?php
44
		} );
45
	}
46
47
	protected function has_major_update() {
48
		$all_updates = get_plugin_updates();
49
		$carbon_fields_data = isset( $all_updates[\Carbon_Fields\RELATIVE_PLUGIN_FILE] ) ? $all_updates[\Carbon_Fields\RELATIVE_PLUGIN_FILE] : null;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
50
		if ( !$carbon_fields_data ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
51
			return false;
52
		}
53
54
		$plugin_data = (object) _get_plugin_data_markup_translate( \Carbon_Fields\RELATIVE_PLUGIN_FILE, (array) $carbon_fields_data, false, true );
55
		$current_version = implode( '.', array_slice( explode( '.', $plugin_data->Version ), 0, 1 ) );
56
		$update_version = implode( '.', array_slice( explode( '.', $plugin_data->update->new_version ), 0, 1 ) );
57
		return version_compare( $current_version, $update_version, '<' ); // compare only MAJOR part of SemVer 
58
	}
59
60
	protected function get_json_data() {
61
		return array(
62
			'plugin_path' => \Carbon_Fields\RELATIVE_PLUGIN_FILE,
63
		);
64
	}
65
66
	protected function template() {
67
		?>
68
		<div class="update-message notice inline notice-error notice-alt">
69
			<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>
1 ignored issue
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'nl2br'
Loading history...
70
		</div>
71
		<?php
72
	}
73
74
	public function enqueue_scripts() {
75
		wp_enqueue_style( 'carbon-plugin-update-warning', \Carbon_Fields\URL . '/core/Libraries/Plugin_Update_Warning/assets/css/app.css', array(), \Carbon_Fields\VERSION );
76
		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 );
77
		wp_localize_script( 'carbon-plugin-update-warning', 'carbonFieldsPluginUpdateWarning', $this->get_json_data() );
78
	}
79
}
80