Completed
Push — development ( 455864...381f0e )
by
unknown
06:32
created

Plugin_Update_Warning   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 98
rs 10
wmc 12
lcom 0
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 8 2
A setup() 0 19 3
A has_major_update() 0 12 3
A get_json_data() 0 5 1
A template() 0 7 1
A print_json_data() 0 9 1
A enqueue_scripts() 0 9 1
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' ) ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
33
			return;
34
		}
35
36
		if ( !$this->has_major_update() ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
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;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
52
		if ( !$carbon_fields_data ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
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>
1 ignored issue
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'nl2br'
Loading history...
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(
0 ignored issues
show
introduced by
Empty array declaration must have no space between the parentheses
Loading history...
103
				
104
			)
105
		);
106
	}
107
}
108