-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathclass-gf-cli-tool.php
More file actions
157 lines (147 loc) · 4.21 KB
/
class-gf-cli-tool.php
File metadata and controls
157 lines (147 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
defined( 'ABSPATH' ) || defined( 'WP_CLI' ) || die();
/**
* Misc Gravity Forms Tools.
*
* @since 1.0
* @package GravityForms/CLI
* @category CLI
* @author Rockegenius
* @copyright Copyright (c) 2016-2018, Rocketgenius
*/
class GF_CLI_Tool extends WP_CLI_Command {
/**
* Clears the Gravity Forms transients.
*
* @since 1.0
* @access public
*
* @alias clear-transients
*/
public function clear_transients( $args, $assoc_args ) {
$result = GFCache::flush( true );
if ( $result ) {
WP_CLI::success( 'Gravity Forms transients cleared successfully.' );
} else {
WP_CLI::error( 'There was a problem clearing the Gravity Forms transients.' );
}
}
/**
* Delete the trashed entries.
*
* @since 1.0-beta-1
* @access public
*
* [<form-id>]
* : The ID of the form. Default: all forms.
*
*
* @subcommand empty-trash
*/
public function empty_trash( $args, $assoc_args ) {
$form_id = isset( $args[0] ) ? $args[0] : 0;
if ( empty( $form_id ) ) {
$form_ids = GFFormsModel::get_form_ids();
foreach ( $form_ids as $id ) {
GFFormsModel::delete_leads_by_form( $id, 'trash' );
}
WP_CLI::success( 'Trash emptied successfully for all forms.' );
} else {
GFFormsModel::delete_leads_by_form( $form_id, 'trash' );
WP_CLI::success( 'Trash emptied successfully for form ID ' . $form_id );
}
}
/**
* Verify Gravity Forms files against the checksums.
*
* Specify version to verify checksums.
*
* @since 1.0-beta-1
* @access public
*
* [--version=<version>]
* : Verify checksums against a specific version of Gravity Forms.
*
* @subcommand verify-checksums
*/
public function verify_checksums( $args, $assoc_args ) {
if ( ! empty( $assoc_args['version'] ) ) {
$gf_version = $assoc_args['version'];
} else {
$gf_version = GFForms::$version;
}
$checksums = $this->get_checksums( $gf_version );
if ( ! is_array( $checksums ) ) {
WP_CLI::error( "Couldn't get download checksums." );
}
$has_errors = false;
foreach ( $checksums as $checksum_string ) {
list( $checksum, $file ) = explode( ' ', $checksum_string );
$path = GFCommon::get_base_path() . DIRECTORY_SEPARATOR . $file;
if ( ! file_exists( $path ) ) {
WP_CLI::warning( "File doesn't exist: {$file}" );
$has_errors = true;
continue;
}
$hashed_file = '';
if ( strlen( $checksum ) === 32 ) {
$hashed_file = md5_file( $path );
} elseif ( strlen( $checksum ) === 40 ) {
$hashed_file = sha1_file( $path );
}
if ( $hashed_file !== $checksum ) {
WP_CLI::warning( "File doesn't verify against checksum: {$file}" );
$has_errors = true;
}
}
if ( ! $has_errors ) {
WP_CLI::success( 'Gravity Forms install verifies against checksums.' );
} else {
WP_CLI::error( 'Gravity Forms install doesn\'t verify against checksums.' );
}
}
/**
* Gets the checksums for the given version of Gravity Forms.
*
* @since 1.0-beta-1
* @access public
*
* @param string $version Version string to query.
*
* @return bool|array False on failure. An array of checksums on success.
*/
private function get_checksums( $version ) {
$url = 'https://s3.amazonaws.com/gravityforms/releases/checksums/gravityforms_' . $version . '.md5';
$options = array(
'timeout' => 30,
);
$headers = array(
'Accept' => 'text/plain',
);
$response = \WP_CLI\Utils\http_request( 'GET', $url, null, $headers, $options );
if ( ! $response->success || 200 != $response->status_code ) {
return false;
}
$body = trim( $response->body );
$checksums = explode( "\n", $body );
return $checksums;
}
/**
* Outputs the system report from the Forms > System Status page.
*
* @since 1.0.3
*
* @alias status
* @subcommand system-report
*/
public function system_report() {
if ( gf_cli()->is_gravityforms_supported( '2.2' ) ) {
require_once( GFCommon::get_base_path() . '/includes/system-status/class-gf-system-report.php' );
$sections = GF_System_Report::get_system_report();
$system_report_text = GF_System_Report::get_system_report_text( $sections );
WP_CLI::success( $system_report_text );
} else {
WP_CLI::error( 'The system report is only available with Gravity Forms 2.2 or greater.' );
}
}
}