Completed
Push — docker/disable-jetpack-update-... ( 41921e )
by
unknown
18:25
created

avoid-plugin-deletion.php ➔ jetpack_docker_disable_plugin_update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
Plugin Name: Disable deleting and updating Jetpack
5
Description: Disable deleting and updating -actions for Jetpack plugin. Being able to delete your local development directory from WordPress is catastrophic and you can lose your git history in the process.
6
Version: 2.0
7
Author: Automattic
8
Author URI: http://automattic.com/
9
*/
10
11
// These are the plugins we don't want to update or delete
12
$jetpack_docker_avoided_plugins = array(
13
	'jetpack/jetpack.php',
14
);
15
16
/**
17
 * Remove the Delete link from your plugins list for important plugins
18
 */
19
function jetpack_docker_disable_plugin_deletion_link( $actions, $plugin_file, $plugin_data, $context ) {
20
	global $jetpack_docker_avoided_plugins;
21
	if (
22
		array_key_exists( 'delete', $actions ) &&
23
		in_array(
24
			$plugin_file,
25
			$jetpack_docker_avoided_plugins
26
		)
27
	) {
28
		unset( $actions['delete'] );
29
	}
30
	return $actions;
31
}
32
add_filter( 'plugin_action_links', 'jetpack_docker_disable_plugin_deletion_link', 10, 4 );
33
34
/**
35
 * Fail deletion attempts of our important plugins
36
 */
37
function jetpack_docker_disable_delete_plugin( $plugin_file ) {
38
	if ( in_array( $plugin_file, $jetpack_docker_avoided_plugins ) ) {
0 ignored issues
show
Bug introduced by
The variable $jetpack_docker_avoided_plugins does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
39
		wp_die(
40
			'Deleting plugin "' . $plugin_file . '" is diabled at mu-plugins/avoid-plugin-deletion.php',
41
			403
42
		);
43
	}
44
}
45
add_action( 'delete_plugin', 'jetpack_docker_disable_delete_plugin', 10, 2 );
46
47
/**
48
 * Stop WordPress noticing plugin updates for important plugins
49
 */
50
function jetpack_docker_disable_plugin_update( $plugins ) {
51
	global $jetpack_docker_avoided_plugins;
52
	foreach( $jetpack_docker_avoided_plugins as $avoided_plugin ) {
53
		if ( isset( $plugins->response[ $avoided_plugin ] ) ) {
54
			unset( $plugins->response[ $avoided_plugin ] );
55
		}
56
	}
57
	return $plugins;
58
}
59
add_filter( 'site_transient_update_plugins', 'jetpack_docker_disable_plugin_update' );
60