Completed
Pull Request — master (#778)
by Zack
07:56 queued 03:54
created

uninstall.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 21 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Fired when the plugin is uninstalled.
4
 *
5
 * @package   GravityView
6
 * @author    Zack Katz <[email protected]>
7
 * @license   ToBeDefined
8
 * @link      http://gravityview.co
9
 * @copyright Copyright 2015, Katz Web Services, Inc.
10
 */
11
12
// If uninstall not called from WordPress, then exit
13
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Delete GravityView content when GravityView is uninstalled, if the setting is set to "Delete on Uninstall"
19
 * @since 1.15
20
 */
21
class GravityView_Uninstall {
22
23
	private $settings_name = 'gravityformsaddon_gravityview_app_settings';
24
25
	public function __construct() {
26
27
		/** @define "$file_path" "./" */
28
		$file_path = plugin_dir_path( __FILE__ );
29
30
		include_once $file_path . 'includes/class-gravityview-roles-capabilities.php';
31
32
		/**
33
		 * Only delete content and settings if "Delete on Uninstall?" setting is "Permanently Delete"
34
		 */
35
		$delete = $this->get_delete_setting();
36
37
		if( GravityView_Roles_Capabilities::has_cap( 'gravityview_uninstall' ) && 'delete' === $delete ) {
38
			$this->fire_everything();
39
		}
40
	}
41
42
	/**
43
	 * Get the GravityView setting for whether to delete all View settings on uninstall
44
	 *
45
	 * @since 1.15
46
	 *
47
	 * @return string|null Returns NULL if not configured (pre-1.15 settings); "0" if false, "delete" if delete
48
	 */
49
	private function get_delete_setting() {
50
51
		$settings = get_option( $this->settings_name, array() );
52
53
		return isset( $settings[ 'delete-on-uninstall' ] ) ? $settings[ 'delete-on-uninstall' ] : null;
0 ignored issues
show
Array keys should NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
54
	}
55
56
	/**
57
	 * Delete GravityView Views, settings, roles, caps, etc.
58
	 * @see https://youtu.be/FXy_DO6IZOA?t=35s
59
	 * @since 1.15
60
	 * @return void
61
	 */
62
	private function fire_everything() {
63
		$this->delete_options();
64
		$this->delete_posts();
65
		$this->delete_capabilities();
66
		$this->delete_entry_meta();
67
		$this->delete_entry_notes();
68
	}
69
70
	/**
71
	 * Delete GravityView "approved entry" meta
72
	 * @since 1.15
73
	 * @return void
74
	 */
75
	private function delete_entry_meta() {
76
		global $wpdb;
77
78
		$meta_table = class_exists( 'GFFormsModel' ) ? GFFormsModel::get_lead_meta_table_name() : $wpdb->prefix . 'rg_lead_meta';
79
80
		$sql = "
81
			DELETE FROM $meta_table
82
			WHERE (
83
				`meta_key` = 'is_approved'
84
			);
85
		";
86
87
		$wpdb->query( $sql );
88
	}
89
90
	/**
91
	 * Delete all GravityView-generated entry notes
92
	 * @since 1.15
93
	 * @return void
94
	 */
95
	private function delete_entry_notes() {
96
		global $wpdb;
97
98
		$notes_table = class_exists( 'GFFormsModel' ) ? GFFormsModel::get_lead_notes_table_name() : $wpdb->prefix . 'rg_lead_notes';
99
100
		$disapproved = __('Disapproved the Entry for GravityView', 'gravityview');
101
		$approved = __('Approved the Entry for GravityView', 'gravityview');
102
103
		$sql = $wpdb->prepare( "
104
			DELETE FROM $notes_table
105
            WHERE (
106
                `note_type` = 'gravityview' OR
107
				`value` = %s OR
108
				`value` = %s
109
            );
110
        ", $approved, $disapproved );
111
112
		$wpdb->query( $sql );
113
	}
114
115
	/**
116
	 * Delete capabilities added by GravityView
117
	 * @since 1.15
118
	 * @return void
119
	 */
120
	private function delete_capabilities() {
121
		GravityView_Roles_Capabilities::get_instance()->remove_caps();
122
	}
123
124
	/**
125
	 * Delete all the GravityView custom post type posts
126
	 * @since 1.15
127
	 * @return void
128
	 */
129
	private function delete_posts() {
130
131
		$items = get_posts( array(
132
			'post_type' => 'gravityview',
133
			'post_status' => 'any',
134
			'numberposts' => -1,
135
			'fields' => 'ids'
136
		) );
137
138
		if ( $items ) {
139
			foreach ( $items as $item ) {
140
				wp_delete_post( $item, true );
141
			}
142
		}
143
	}
144
145
	/**
146
	 * Delete GravityView options
147
	 * @since 1.15
148
	 * @return void
149
	 */
150
	private function delete_options() {
151
152
		delete_option( 'gravityformsaddon_gravityview_app_settings' );
153
		delete_option( 'gravityformsaddon_gravityview_version' );
154
		delete_option( 'gravityview_cache_blacklist' );
155
156
		delete_transient( 'gravityview_edd-activate_valid' );
157
		delete_transient( 'gravityview_edd-deactivate_valid' );
158
		delete_transient( 'gravityview_dismissed_notices' );
159
160
		delete_site_transient( 'gravityview_related_plugins' );
161
	}
162
}
163
164
new GravityView_Uninstall;