Completed
Push — master ( 1fd6be...155036 )
by Zack
13s
created

GravityView_Uninstall::fire_everything()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0
1
<?php
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
/**
13
 * Delete GravityView content when GravityView is uninstalled, if the setting is set to "Delete on Uninstall"
14
 * @since 1.15
15
 */
16
class GravityView_Uninstall {
17
18
	/**
19
	 * Delete GravityView Views, settings, roles, caps, etc.
20
	 * @see https://youtu.be/FXy_DO6IZOA?t=35s
21
	 * @since 1.15
22
	 * @return void
23
	 */
24 1
	public function fire_everything() {
25 1
		$this->delete_posts();
26 1
		$this->delete_capabilities();
27 1
		$this->delete_entry_meta();
28 1
		$this->delete_entry_notes();
29
30
		// Keep this as last to make sure the GravityView Cache blacklist option is deleted
31 1
		$this->delete_options();
32 1
	}
33
34
	/**
35
	 * Delete GravityView "approved entry" meta
36
	 * @since 1.15
37
	 * @return void
38
	 */
39
	private function delete_entry_meta() {
40
		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...
41
42
		$tables = array();
43
44
		if ( version_compare( GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) {
45
			$tables []= GFFormsModel::get_entry_meta_table_name();
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
46
		}
47
		$tables []= GFFormsModel::get_lead_meta_table_name();
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
48
49
		$suppress = $wpdb->suppress_errors();
50
		foreach ( $tables as $meta_table ) {
51
			$sql = "
52
				DELETE FROM $meta_table
53
				WHERE (
54
					`meta_key` = 'is_approved'
55
				);
56
			";
57
			$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...
58
		}
59
		$wpdb->suppress_errors( $suppress );
60
	}
61
62
	/**
63
	 * Delete all GravityView-generated entry notes
64
	 * @since 1.15
65
	 * @return void
66
	 */
67
	private function delete_entry_notes() {
68
		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...
69
70
		$tables = array();
71
72
		if ( version_compare( GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) && method_exists( 'GFFormsModel', 'get_entry_notes_table_name' ) ) {
73
			$tables[] = GFFormsModel::get_entry_notes_table_name();
74
		}
75
76
		$tables[] = GFFormsModel::get_lead_notes_table_name();
77
78
		$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...
79
		$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...
80
81
		$suppress = $wpdb->suppress_errors();
82
		foreach ( $tables as $notes_table ) {
83
			$sql = $wpdb->prepare( "
84
				DELETE FROM $notes_table
85
				WHERE (
86
					`note_type` = 'gravityview' OR
87
					`value` = %s OR
88
					`value` = %s
89
				);
90
			", $approved, $disapproved );
91
			$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...
92
		}
93
		$wpdb->suppress_errors( $suppress );
94
	}
95
96
	/**
97
	 * Delete capabilities added by GravityView
98
	 * @since 1.15
99
	 * @return void
100
	 */
101
	private function delete_capabilities() {
102
		GravityView_Roles_Capabilities::get_instance()->remove_caps();
103
	}
104
105
	/**
106
	 * Delete all the GravityView custom post type posts
107
	 * @since 1.15
108
	 * @return void
109
	 */
110
	private function delete_posts() {
111
112
		$items = get_posts( array(
113
			'post_type' => 'gravityview',
114
			'post_status' => 'any',
115
			'numberposts' => -1,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set numberposts to -1 ever.
Loading history...
116
			'fields' => 'ids'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
117
		) );
118
119
		if ( $items ) {
120
			foreach ( $items as $item ) {
121
				wp_delete_post( $item, true );
122
			}
123
		}
124
	}
125
126
	/**
127
	 * Delete GravityView options
128
	 * @since 1.15
129
	 * @return void
130
	 */
131
	private function delete_options() {
132
		delete_option( 'gravityview_cache_blacklist' );
133
		delete_option( 'gv_version_upgraded_from' );
134
		delete_transient( 'gravityview_edd-activate_valid' );
135
		delete_transient( 'gravityview_edd-deactivate_valid' );
136
		delete_transient( 'gravityview_dismissed_notices' );
137
		delete_site_transient( 'gravityview_related_plugins' );
138
	}
139
}