Completed
Push — develop ( ab6671...011f6a )
by
unknown
07:54
created

GravityView_Uninstall   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 17.5%

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 7
cts 40
cp 0.175
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fire_everything() 0 9 1
A delete_capabilities() 0 3 1
A delete_posts() 0 15 3
A delete_options() 0 8 1
A delete_entry_meta() 0 14 2
A delete_entry_notes() 0 19 2
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
		$meta_table = class_exists( 'GFFormsModel' ) ? GFFormsModel::get_lead_meta_table_name() : $wpdb->prefix . 'rg_lead_meta';
43
44
		$sql = "
45
			DELETE FROM $meta_table
46
			WHERE (
47
				`meta_key` = 'is_approved'
48
			);
49
		";
50
51
		$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...
52
	}
53
54
	/**
55
	 * Delete all GravityView-generated entry notes
56
	 * @since 1.15
57
	 * @return void
58
	 */
59
	private function delete_entry_notes() {
60
		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...
61
62
		$notes_table = class_exists( 'GFFormsModel' ) ? GFFormsModel::get_lead_notes_table_name() : $wpdb->prefix . 'rg_lead_notes';
63
64
		$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...
65
		$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...
66
67
		$sql = $wpdb->prepare( "
68
			DELETE FROM $notes_table
69
            WHERE (
70
                `note_type` = 'gravityview' OR
71
				`value` = %s OR
72
				`value` = %s
73
            );
74
        ", $approved, $disapproved );
75
76
		$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...
77
	}
78
79
	/**
80
	 * Delete capabilities added by GravityView
81
	 * @since 1.15
82
	 * @return void
83
	 */
84
	private function delete_capabilities() {
85
		GravityView_Roles_Capabilities::get_instance()->remove_caps();
86
	}
87
88
	/**
89
	 * Delete all the GravityView custom post type posts
90
	 * @since 1.15
91
	 * @return void
92
	 */
93
	private function delete_posts() {
94
95
		$items = get_posts( array(
96
			'post_type' => 'gravityview',
97
			'post_status' => 'any',
98
			'numberposts' => -1,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set numberposts to -1 ever.
Loading history...
99
			'fields' => 'ids'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
100
		) );
101
102
		if ( $items ) {
103
			foreach ( $items as $item ) {
104
				wp_delete_post( $item, true );
105
			}
106
		}
107
	}
108
109
	/**
110
	 * Delete GravityView options
111
	 * @since 1.15
112
	 * @return void
113
	 */
114
	private function delete_options() {
115
		delete_option( 'gravityview_cache_blacklist' );
116
		delete_option( 'gv_version_upgraded_from' );
117
		delete_transient( 'gravityview_edd-activate_valid' );
118
		delete_transient( 'gravityview_edd-deactivate_valid' );
119
		delete_transient( 'gravityview_dismissed_notices' );
120
		delete_site_transient( 'gravityview_related_plugins' );
121
	}
122
}