Completed
Push — develop ( 363d85...76f199 )
by Zack
04:46
created

includes/admin/class-gravityview-support-port.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 6 and the first side effect is on line 310.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * @since 1.15
5
 */
6
class GravityView_Support_Port {
7
8
	/**
9
	 * @var string The name of the User Meta option used to store whether a user wants to see the Support Port
10
	 * @since 1.15
11
	 */
12
	const user_pref_name = 'gravityview_support_port';
13
14
	/**
15
	 * @var string Key used to store active GravityView/Gravity Forms plugin data
16
	 * @since 1.15
17
	 */
18
	const related_plugins_key = 'gravityview_related_plugins';
19
20
	public function __construct() {
21
		$this->add_hooks();
22
	}
23
24
	/**
25
	 * @since 1.15
26
	 */
27
	private function add_hooks() {
28
		add_action( 'personal_options', array( $this, 'user_field' ) );
29
		add_action( 'personal_options_update', array( $this, 'update_user_meta_value' ) );
30
		add_action( 'edit_user_profile_update', array( $this, 'update_user_meta_value' ) );
31
		add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_script' ), 1000 );
32
		add_action( 'update_option_active_plugins', array( $this, 'flush_related_plugins_transient' ) );
33
		add_action( 'update_option_active_sitewide_plugins', array( $this, 'flush_related_plugins_transient' ) );
34
	}
35
36
	/**
37
	 * Enqueue Support Port script if user has it enabled and we're on a GravityView plugin page
38
	 *
39
	 * @uses gravityview_is_admin_page()
40
	 * @uses wp_enqueue_script()
41
	 * @since 1.15
42
	 *
43
	 * @return void
44
	 */
45
	public static function maybe_enqueue_script( $hook ) {
46
		global $pagenow;
47
48
		// Don't show if not GravityView page, or if we're on the Widgets page
49
		if ( ! gravityview_is_admin_page( $hook ) || $pagenow === 'widgets.php' ) {
50
			return;
51
		}
52
53
		/**
54
		 * @filter `gravityview/support_port/display` Whether to display Support Port
55
		 * @since 1.15
56
		 * @param boolean $display_beacon Default: `true`
57
		 */
58
		$display_support_port = apply_filters( 'gravityview/support_port/display', self::show_for_user() );
59
60
		if ( empty( $display_support_port ) ) {
61
			do_action( 'gravityview_log_debug', __METHOD__ . ' - Not showing Support Port' );
62
63
			return;
64
		}
65
66
		$script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
67
68
		wp_enqueue_script( 'gravityview-support', plugins_url( 'assets/js/support' . $script_debug . '.js', GRAVITYVIEW_FILE ), array(), GravityView_Plugin::version, true );
69
70
		self::_localize_script();
71
	}
72
73
	/**
74
	 * Localize the Support Port script
75
	 *
76
	 * @uses wp_localize_script()
77
	 * @since 1.15
78
	 * @return void
79
	 */
80
	private static function _localize_script() {
81
82
		$translation = array(
83
			'agentLabel'                => __( 'GravityView Support', 'gravityview' ),
84
			'searchLabel'               => __( 'Search GravityView Docs', 'gravityview' ),
85
			'searchErrorLabel'          => __( 'Your search timed out. Please double-check your internet connection and try again.', 'gravityview' ),
86
			'noResultsLabel'            => _x( 'No results found for', 'a support form search has returned empty for the following word', 'gravityview' ),
87
			'contactLabel'              => __( 'Contact Support', 'gravityview' ),
88
			'attachFileLabel'           => __( 'Attach a screenshot or file', 'gravityview' ),
89
			'attachFileError'           => __( 'The maximum file size is 10 MB', 'gravityview' ),
90
			'nameLabel'                 => __( 'Your Name', 'gravityview' ),
91
			'nameError'                 => __( 'Please enter your name', 'gravityview' ),
92
			'emailLabel'                => __( 'Email address', 'gravityview' ),
93
			'emailError'                => __( 'Please enter a valid email address', 'gravityview' ),
94
			'subjectLabel'              => __( 'Subject', 'gravityview' ),
95
			'subjectError'              => _x( 'Please enter a subject', 'Error shown when submitting support request and there is no subject provided', 'gravityview' ),
96
			'messageLabel'              => __( 'How can we help you?', 'gravityview' ),
97
			'messageError'              => _x( 'Please enter a message', 'Error shown when submitting support request and there is no message provided', 'gravityview' ),
98
			'contactSuccessLabel'       => __( 'Message sent!', 'gravityview' ),
99
			'contactSuccessDescription' => __( 'Thanks for reaching out! Someone from the GravityView team will get back to you soon.', 'gravityview' ),
100
		);
101
102
		$response = GravityView_Settings::getSetting( 'license_key_response' );
103
104
		$response = wp_parse_args( $response, array(
105
			'license'          => '',
106
			'message'          => '',
107
			'license_key'      => '',
108
			'license_limit'    => '',
109
			'expires'          => '',
110
			'activations_left' => '',
111
			'site_count'       => '',
112
			'payment_id'       => '',
113
			'customer_name'    => '',
114
			'customer_email'   => '',
115
		) );
116
117
		// This is just HTML we don't need.
118
		unset( $response['message'] );
119
120
		switch ( intval( $response['license_limit'] ) ) {
121
			case 1:
122
				$package = 'Sol';
123
				break;
124
			case 100:
125
				$package = 'Galactic';
126
				break;
127
			case 3:
128
				$package = 'Interstellar';
129
				break;
130
			default:
131
				$package = sprintf( '%d-Site License', $response['license_limit'] );
132
		}
133
134
		$data = array(
135
			'email'                 => GravityView_Settings::getSetting( 'support-email' ),
136
			'name'                  => $response['customer_name'],
137
			'Valid License?'        => ucwords( $response['license'] ),
138
			'License Key'           => $response['license_key'],
139
			'License Level'         => $package,
140
			'Site Admin Email'      => get_bloginfo( 'admin_email' ),
141
			'Support Email'         => GravityView_Settings::getSetting( 'support-email' ),
142
			'License Limit'         => $response['license_limit'],
143
			'Site Count'            => $response['site_count'],
144
			'License Expires'       => $response['expires'],
145
			'Activations Left'      => $response['activations_left'],
146
			'Payment ID'            => $response['payment_id'],
147
			'Payment Name'          => $response['customer_name'],
148
			'Payment Email'         => $response['customer_email'],
149
			'WordPress Version'     => get_bloginfo( 'version', 'display' ),
150
			'PHP Version'           => phpversion(),
151
			'GravityView Version'   => GravityView_Plugin::version,
152
			'Gravity Forms Version' => GFForms::$version,
153
			'Plugins & Extensions'  => self::get_related_plugins_and_extensions(),
154
		);
155
156
		$localization_data = array(
157
			'contactEnabled' => (int)current_user_can( 'gravityview_contact_support' ), // @todo use GVCommon::has_cap() after merge
158
			'protocol' => ( is_ssl() ? 'https' : 'http' ),
159
			'data' => $data,
160
			'translation' => $translation,
161
		);
162
163
		wp_localize_script( 'gravityview-support', 'gvSupport', $localization_data );
164
165
		unset( $localization_data, $data, $translation, $response, $package );
166
	}
167
168
	/**
169
	 * Get active GravityView Extensions and Gravity Forms Add-ons to help debug issues.
170
	 *
171
	 * @since 1.15
172
	 * @return string List of active extensions related to GravityView or Gravity Forms, separated by HTML line breaks
173
	 */
174
	static private function get_related_plugins_and_extensions() {
175
176
		if ( ! function_exists( 'wp_get_active_and_valid_plugins' ) ) {
177
			return 'Running < WP 3.0';
178
		}
179
180
		$extensions = get_site_transient( self::related_plugins_key );
181
182
		if ( empty( $extensions ) ) {
183
184
			$active_plugins = wp_get_active_and_valid_plugins();
185
			$extensions = array();
186
			foreach ( $active_plugins as $active_plugin ) {
187
188
				// Match gravityview, gravity-forms, gravityforms, gravitate
189
				if ( ! preg_match( '/(gravityview|gravity-?forms|gravitate)/ism', $active_plugin ) ) {
190
					continue;
191
				}
192
193
				$plugin_data = get_plugin_data( $active_plugin );
194
195
				$extensions[] = sprintf( '%s %s', $plugin_data['Name'], $plugin_data['Version'] );
196
			}
197
198
			if( ! empty( $extensions ) ) {
199
				set_site_transient( self::related_plugins_key, $extensions, HOUR_IN_SECONDS );
200
			} else {
201
				return 'There was an error fetching related plugins.';
202
			}
203
		}
204
		
205
		return implode( '<br />', $extensions );
206
	}
207
208
	/**
209
	 * When a plugin is activated or deactivated, delete the cached extensions/plugins used by get_related_plugins_and_extensions()
210
	 *
211
	 * @see get_related_plugins_and_extensions()
212
	 * @since 1.15
213
	 */
214
	public function flush_related_plugins_transient() {
215
		if ( function_exists( 'delete_site_transient' ) ) {
216
			delete_site_transient( self::related_plugins_key );
217
		}
218
	}
219
220
	/**
221
	 * Check whether to show Support for a user
222
	 *
223
	 * If the user doesn't have the `gravityview_support_port` capability, returns false.
224
	 * If there is no preference set for the user, use the global plugin setting.
225
	 *
226
	 * @since 1.15
227
	 *
228
	 * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
229
	 *
230
	 * @return bool Whether to show GravityView support
231
	 */
232
	static public function show_for_user( $user = 0 ) {
233
234
		if ( ! GVCommon::has_cap( 'gravityview_support_port' ) ) {
235
			return false;
236
		}
237
238
		$pref = get_user_option( self::user_pref_name, $user );
239
240
		// Not set; default to plugin setting
241
		if ( false === $pref ) {
242
			return GravityView_Settings::getSetting( 'support_port' );
243
		}
244
245
		return ! empty( $pref );
246
	}
247
248
249
	/**
250
	 * Update User Profile preferences for GravityView Support
251
	 *
252
	 * @since 1.5
253
	 *
254
	 * @param int $user_id
255
	 *
256
	 * @return void
257
	 */
258
	public function update_user_meta_value( $user_id ) {
259
		if ( current_user_can( 'edit_user', $user_id ) && isset( $_POST[ self::user_pref_name ] ) ) {
260
			update_user_meta( $user_id, self::user_pref_name, intval( $_POST[ self::user_pref_name ] ) );
261
		}
262
	}
263
264
	/**
265
	 * Modify User Profile
266
	 *
267
	 * Modifies the output of profile.php to add GravityView Support preference
268
	 *
269
	 * @since 1.15
270
	 *
271
	 * @param WP_User $user Current user info
272
	 *
273
	 * @return void
274
	 */
275
	public function user_field( $user ) {
276
277
		/**
278
		 * @filter `gravityview/support_port/show_profile_setting` Should the "GravityView Support Port" setting be shown on user profiles?
279
		 * @todo use GVCommon::has_cap() after merge
280
		 * @since 1.15
281
		 *
282
		 * @param boolean $allow_profile_setting Default: `true`, if the user has the `gravityview_support_port` capability, which defaults to true for Contributors and higher
283
		 * @param WP_User $user Current user object
284
		 */
285
		$allow_profile_setting = apply_filters( 'gravityview/support_port/show_profile_setting', current_user_can( 'gravityview_support_port' ), $user );
286
287
		if ( $allow_profile_setting && current_user_can( 'edit_user', $user->ID ) ) {
288
			?>
289
			<table class="form-table">
290
				<tbody>
291
					<tr class="user-gravityview-support-button-wrap">
292
						<th scope="row"><?php _e( 'GravityView Support Port', 'gravityview' ); ?></th>
293
						<td>
294
							<fieldset>
295
								<legend class="screen-reader-text"><span><?php _e( 'GravityView Support Port', 'gravityview' ) ?></span></legend>
296
								<label>
297
									<input name="<?php echo esc_attr( self::user_pref_name ); ?>" type="hidden" value="0"/>
298
									<input name="<?php echo esc_attr( self::user_pref_name ); ?>" type="checkbox" value="1" <?php checked( self::show_for_user( $user->ID ) ); ?> />
299
									<?php esc_html_e( 'Show GravityView Support Port when on a GravityView-related page', 'gravityview' ); ?>
300
								</label>
301
							</fieldset>
302
						</td>
303
					</tr>
304
				</tbody>
305
			</table>
306
		<?php }
307
	}
308
}
309
310
new GravityView_Support_Port;