Completed
Push — master ( 9cf7be...029c15 )
by Zack
04:07
created

GravityView_Uninstall::delete_options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
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 ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
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
introduced by
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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 );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
101
		$approved = __('Approved the Entry for GravityView', 'gravityview');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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 );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
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,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set numberposts to -1 ever.
Loading history...
135
			'fields' => 'ids'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
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;