Completed
Push — master ( cf9740...0c1274 )
by Zack
19:03 queued 03:33
created

GravityView_Uninstall::delete_entry_meta()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 16
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 26
ccs 0
cts 15
cp 0
crap 20
rs 8.5806
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 ( method_exists( 'GFFormsModel', 'get_entry_meta_table_name' ) ) {
45
			$tables []= GFFormsModel::get_entry_meta_table_name();
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
46
		} else if ( method_exists( 'GFFormsModel', 'get_lead_meta_table_name' ) ) {
47
			$tables []= GFFormsModel::get_lead_meta_table_name();
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
48
		} else {
49
			$tables []= $wpdb->prefix . 'rg_lead_meta';
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
50
			$tables []= $wpdb->prefix . 'gf_entry_meta';
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
51
		}
52
53
		$suppress = $wpdb->suppress_errors();
54
		foreach ( $tables as $meta_table ) {
55
			$sql = "
56
				DELETE FROM $meta_table
57
				WHERE (
58
					`meta_key` = 'is_approved'
59
				);
60
			";
61
			$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...
62
		}
63
		$wpdb->suppress_errors( $suppress );
64
	}
65
66
	/**
67
	 * Delete all GravityView-generated entry notes
68
	 * @since 1.15
69
	 * @return void
70
	 */
71
	private function delete_entry_notes() {
72
		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...
73
74
		$tables = array();
75
76
		if ( method_exists( 'GFFormsModel', 'get_entry_notes_table_name' ) ) {
77
			$tables []= GFFormsModel::get_entry_notes_table_name();
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
78
		} else if ( method_exists( 'GFFormsModel', 'get_lead_notes_table_name' ) ) {
79
			$tables []= GFFormsModel::get_lead_notes_table_name();
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
80
		} else {
81
			$tables []= $wpdb->prefix . 'rg_lead_notes';
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
82
			$tables []= $wpdb->prefix . 'gf_entry_notes';
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
83
		}
84
85
		$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...
86
		$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...
87
88
		$suppress = $wpdb->suppress_errors();
89
		foreach ( $tables as $notes_table ) {
90
			$sql = $wpdb->prepare( "
91
				DELETE FROM $notes_table
92
				WHERE (
93
					`note_type` = 'gravityview' OR
94
					`value` = %s OR
95
					`value` = %s
96
				);
97
			", $approved, $disapproved );
98
			$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...
99
		}
100
		$wpdb->suppress_errors( $suppress );
101
	}
102
103
	/**
104
	 * Delete capabilities added by GravityView
105
	 * @since 1.15
106
	 * @return void
107
	 */
108
	private function delete_capabilities() {
109
		GravityView_Roles_Capabilities::get_instance()->remove_caps();
110
	}
111
112
	/**
113
	 * Delete all the GravityView custom post type posts
114
	 * @since 1.15
115
	 * @return void
116
	 */
117
	private function delete_posts() {
118
119
		$items = get_posts( array(
120
			'post_type' => 'gravityview',
121
			'post_status' => 'any',
122
			'numberposts' => -1,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set numberposts to -1 ever.
Loading history...
123
			'fields' => 'ids'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
124
		) );
125
126
		if ( $items ) {
127
			foreach ( $items as $item ) {
128
				wp_delete_post( $item, true );
129
			}
130
		}
131
	}
132
133
	/**
134
	 * Delete GravityView options
135
	 * @since 1.15
136
	 * @return void
137
	 */
138
	private function delete_options() {
139
		delete_option( 'gravityview_cache_blacklist' );
140
		delete_option( 'gv_version_upgraded_from' );
141
		delete_transient( 'gravityview_edd-activate_valid' );
142
		delete_transient( 'gravityview_edd-deactivate_valid' );
143
		delete_transient( 'gravityview_dismissed_notices' );
144
		delete_site_transient( 'gravityview_related_plugins' );
145
	}
146
}