Completed
Pull Request — master (#936)
by Zack
20:10 queued 09:19
created

GravityView_Uninstall::delete_entry_meta()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 0
dl 0
loc 22
ccs 0
cts 12
cp 0
crap 12
rs 9.2
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( 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( GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) {
73
			$tables []= GFFormsModel::get_entry_notes_table_name();
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
74
		}
75
		$tables []= GFFormsModel::get_lead_notes_table_name();
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
76
77
		$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...
78
		$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...
79
80
		$suppress = $wpdb->suppress_errors();
81
		foreach ( $tables as $notes_table ) {
82
			$sql = $wpdb->prepare( "
83
				DELETE FROM $notes_table
84
				WHERE (
85
					`note_type` = 'gravityview' OR
86
					`value` = %s OR
87
					`value` = %s
88
				);
89
			", $approved, $disapproved );
90
			$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...
91
		}
92
		$wpdb->suppress_errors( $suppress );
93
	}
94
95
	/**
96
	 * Delete capabilities added by GravityView
97
	 * @since 1.15
98
	 * @return void
99
	 */
100
	private function delete_capabilities() {
101
		GravityView_Roles_Capabilities::get_instance()->remove_caps();
102
	}
103
104
	/**
105
	 * Delete all the GravityView custom post type posts
106
	 * @since 1.15
107
	 * @return void
108
	 */
109
	private function delete_posts() {
110
111
		$items = get_posts( array(
112
			'post_type' => 'gravityview',
113
			'post_status' => 'any',
114
			'numberposts' => -1,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set numberposts to -1 ever.
Loading history...
115
			'fields' => 'ids'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
116
		) );
117
118
		if ( $items ) {
119
			foreach ( $items as $item ) {
120
				wp_delete_post( $item, true );
121
			}
122
		}
123
	}
124
125
	/**
126
	 * Delete GravityView options
127
	 * @since 1.15
128
	 * @return void
129
	 */
130
	private function delete_options() {
131
		delete_option( 'gravityview_cache_blacklist' );
132
		delete_option( 'gv_version_upgraded_from' );
133
		delete_transient( 'gravityview_edd-activate_valid' );
134
		delete_transient( 'gravityview_edd-deactivate_valid' );
135
		delete_transient( 'gravityview_dismissed_notices' );
136
		delete_site_transient( 'gravityview_related_plugins' );
137
	}
138
}