Completed
Pull Request — master (#1147)
by Zack
22:38 queued 18:35
created

get_download_display_details()   C

Complexity

Conditions 11
Paths 14

Size

Total Lines 99

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
nc 14
nop 2
dl 0
loc 99
ccs 0
cts 76
cp 0
crap 132
rs 5.8751
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 16 and the first side effect is on line 6.

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
// Exit if accessed directly
4
5
if ( ! defined( 'ABSPATH' ) ) {
6
	exit;
7
}
8
9
/**
10
 * GravityView_Admin_Installer Class
11
 *
12
 * A general class for About page.
13
 *
14
 * @since 2.1
15
 */
16
class GravityView_Admin_Installer {
17
18
	const EDD_API_URL = 'https://gravityview.co/edd-api/products/';
19
20
	const EDD_API_KEY = 'e4c7321c4dcf342c9cb078e27bf4ba97';
21
22
	const EDD_API_TOKEN = 'e031fd350b03bc223b10f04d8b5dde42';
23
24
	const DOWNLOADS_DATA_TRANSIENT = 'gv_downloads_data';
25
26
	const DOWNLOADS_DATA_TRANSIENT_EXPIRY = DAY_IN_SECONDS;
27
28
	/**
29
	 * @var string
30
	 */
31
	public $minimum_capability = 'install_plugins';
32
33
	public function __construct() {
34
35
		$this->add_downloads_data_filters();
36
37
		add_action( 'admin_menu', array( $this, 'add_admin_menu' ), 200 );
38
		add_action( 'gravityview/admin_installer/delete_downloads_data', array( $this, 'delete_downloads_data' ) );
39
		add_action( 'wp_ajax_gravityview_admin_installer_activate', array( $this, 'activate_download' ) );
40
		add_action( 'wp_ajax_gravityview_admin_installer_deactivate', array( $this, 'deactivate_download' ) );
41
		add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_scripts_and_styles' ) );
42
		add_filter( 'gravityview_noconflict_scripts', array( $this, 'register_noconflict' ) );
43
		add_filter( 'gravityview_noconflict_styles', array( $this, 'register_noconflict' ) );
44
		add_filter( 'gravityview/settings/license-key-notice', array( $this, 'maybe_modify_license_notice' ) );
45
	}
46
47
	/**
48
	 * Let us operate when GF no-conflict is enabled
49
	 *
50
	 * @param array $items Scripts or styles to exclude from no-conflict
51
	 *
52
	 * @return array
53
	 */
54
	public function register_noconflict( $items ) {
55
56
		$items[] = 'gravityview-admin-installer';
57
58
		return $items;
59
	}
60
61
62
	/**
63
	 * Modify plugins data with custom GV extension info
64
	 *
65
	 * @return void
66
	 */
67
	public function add_downloads_data_filters() {
68
69
	    $downloads_data = get_site_transient( self::DOWNLOADS_DATA_TRANSIENT );
70
71
	    if ( ! $downloads_data ) {
72
			return;
73
		}
74
75
		add_filter( 'plugins_api', function ( $data, $action, $args ) use ( $downloads_data ) {
76
			foreach ( $downloads_data as $extension ) {
77
				if ( empty( $extension['info'] ) || empty( $args->slug ) || $args->slug !== $extension['info']['slug'] ) {
78
					continue;
79
				}
80
81
				return (object) array(
82
					'slug'          => $extension['info']['slug'],
83
					'name'          => $extension['info']['title'],
84
					'version'       => $extension['licensing']['version'],
85
					'download_link' => $extension['files'][0]['file'],
86
				);
87
			}
88
89
			return $data;
90
		}, 10, 3 );
91
	}
92
93
	/**
94
	 * Add new admin menu
95
	 *
96
	 * @return void
97
	 */
98
	public function add_admin_menu() {
99
100
	    $menu_text = _x( 'Extensions', 'Extensions are WordPress plugins that add functionality to GravityView and Gravity Forms', 'gravityview' );
101
102
		$menu_text = sprintf( '<span title="%s">%s</span>', esc_attr__( 'Plugins that extend GravityView and Gravity Forms functionality.', 'gravityview' ), $menu_text );
103
104
		add_submenu_page(
105
			'edit.php?post_type=gravityview',
106
			__( 'GravityView Extensions and Plugins', 'gravityview' ),
107
			$menu_text,
108
			$this->minimum_capability,
109
			'gv-admin-installer',
110
			array( $this, 'render_screen' )
111
		);
112
	}
113
114
	/**
115
     * When on the Installer page, show a different notice than on the Settings page
116
     *
117
	 * @param array $notice
118
	 *
119
	 * @return string License notice
120
	 */
121
	public function maybe_modify_license_notice( $notice = '' ) {
122
123
		if ( ! gravityview()->request->is_admin( '', 'downloads' ) ) {
0 ignored issues
show
Unused Code introduced by
The call to Request::is_admin() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
124
            return $notice;
125
        }
126
127
        return esc_html__( 'Your license %s. Do you want access to these plugins? %sActivate your license%s or %sget a license here%s.', 'gravityview' );
128
	}
129
130
	/**
131
	 * Get an array of plugins with textdomains as keys
132
	 *
133
	 * @return array {
134
	 * @type string $path Path to the plugin
135
	 * @type string $version What version is the plugin
136
	 * @type bool $activated Is the plugin activated
137
	 * }
138
	 */
139
	protected function get_wp_plugins_data() {
140
141
		$wp_plugins = array();
142
143
		$all_plugins = get_plugins();
144
145
		foreach ( $all_plugins as $path => $plugin ) {
146
147
			if ( empty( $plugin['TextDomain'] ) ) {
148
				continue;
149
			}
150
151
			$wp_plugins[ $plugin['TextDomain'] ] = array(
152
				'path'      => $path,
153
				'version'   => $plugin['Version'],
154
				'activated' => is_plugin_active( $path )
155
			);
156
		}
157
158
		return $wp_plugins;
159
	}
160
161
	/**
162
	 * Get downloads data from transient or from API; save transient after getting data from API
163
	 *
164
	 * @return WP_Error|array If error, returns WP_Error. If not valid JSON, empty array. Otherwise, this structure: {
165
     *   @type array  $info {
166
     *       @type string $id int 17
167
     *       @type string $slug Extension slug
168
     *       @type string $title Extension title
169
     *       @type string $create_date in '2018-07-19 20:03:10' format
170
     *       @type string $modified_date
171
     *       @type string $status
172
     *       @type string $link URL to public plugin page
173
     *       @type string $content
174
     *       @type string $excerpt
175
     *       @type string $thumbnail URL to thumbnail
176
     *       @type array  $category Taxonomy details for the plugin's category {
177
     *         @type int $term_id => int 30
178
     *         @type string $name => string 'Plugins' (length=7)
179
     *         @type string $slug => string 'plugins' (length=7)
180
     *         @type int $term_group => int 0
181
     *         @type int $term_taxonomy_id => int 30
182
     *         @type string $taxonomy => string 'download_category' (length=17)
183
     *         @type string $description => string '' (length=0)
184
     *         @type int $parent => int 0
185
     *         @type int $count => int 4
186
     *         @type string $filter => string 'raw' (length=3)
187
     *       }
188
     *       @type array $tags {see $category above}
189
     *       @type string $textdomain string 'gravityview' (length=11)
190
     *   }
191
     *   @type array $pricing array of `price_name_slugs` => '00.00' values, if price options exist
192
     *   @type array $licensing {
193
     *       @type bool   $enabled Is licensing enabled for the extension
194
     *       @type string $version Version number
195
     *       @type string $exp_unit Expiration unit ('years')
196
     *       @type string $exp_length Expiration length ('1')
197
     *   }
198
     *   @type array $files Array of files. Empty if user has no access to the file. {
199
     *       @type string $file string URL of the file download
200
     *   }
201
     * }
202
	 */
203
	public function get_downloads_data() {
204
205
		$downloads_data = get_site_transient( self::DOWNLOADS_DATA_TRANSIENT );
206
207
		if ( $downloads_data ) {
208
			return $downloads_data;
209
		}
210
211
		$home_url = parse_url( home_url() );
212
213
		$api_url = add_query_arg(
214
			array(
215
				'key'         => self::EDD_API_KEY,
216
				'token'       => self::EDD_API_TOKEN,
217
				'url'         => \GV\Utils::get( $home_url, 'host', home_url() ),
218
				'license_key' => gravityview()->plugin->settings->get( 'license_key' )
219
			),
220
			self::EDD_API_URL
221
		);
222
223
		$response = wp_remote_get( $api_url, array(
0 ignored issues
show
introduced by
wp_remote_get is highly discouraged, please use vip_safe_wp_remote_get() instead.
Loading history...
224
			'sslverify' => false,
225
			'timeout'   => 5,
226
		) );
227
228
		if ( is_wp_error( $response ) ) {
229
		    gravityview()->log->error( "Extension data response is an error", array( 'data' => $response ) );
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Extension data response is an error does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
230
			return $response;
231
		}
232
233
		$downloads_data = json_decode( wp_remote_retrieve_body( $response ), true );
234
235
		if ( empty( $downloads_data['products'] ) ) {
236
			return array();
237
		}
238
239
		$this->set_downloads_data( $downloads_data['products'] );
240
241
		return $downloads_data['products'];
242
	}
243
244
	/**
245
	 * Save downloads data in a time-bound transient
246
	 *
247
	 * @param array $data
248
	 *
249
	 * @return true if successful, false otherwise
250
	 */
251
	public function set_downloads_data( $data ) {
252
		return set_site_transient( self::DOWNLOADS_DATA_TRANSIENT, $data, self::DOWNLOADS_DATA_TRANSIENT_EXPIRY );
253
	}
254
255
	/**
256
	 * Delete downloads data transient
257
	 *
258
	 * @return bool true if successful, false otherwise
259
	 */
260
	public function delete_downloads_data() {
261
		return delete_site_transient( self::DOWNLOADS_DATA_TRANSIENT );
262
	}
263
264
	/**
265
	 * Display a grid of available downloads and controls to install/activate/deactivate them
266
	 *
267
	 * @since 2.1
268
	 *
269
	 * @return void
270
	 */
271
	public function render_screen() {
272
273
		$downloads_data = $this->get_downloads_data();
274
275
		if ( is_wp_error( $downloads_data ) || empty( $downloads_data ) ) {
276
			?>
277
            <div class="wrap">
278
                <h1><?php esc_html_e( 'GravityView Extensions and Plugins', 'gravityview' ); ?></h1>
279
                <div class="gv-admin-installer-notice notice inline error">
280
                    <h3><?php esc_html_e( 'Extensions and plugins data cannot be loaded at the moment. Please try again later.', 'gravityview' ); ?></h3>
281
                    <?php
282
                    if ( is_wp_error( $downloads_data ) ) {
283
	                    echo wpautop( '<pre>' . esc_html( $downloads_data->get_error_message() ) . '</pre>' );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'wpautop'
Loading history...
284
                    }
285
                    ?>
286
                </div>
287
            </div>
288
			<?php
289
290
			return;
291
		}
292
293
		?>
294
        <div class="wrap">
295
296
297
            <h1><?php esc_html_e( 'GravityView Extensions and Plugins', 'gravityview' ); ?></h1>
298
299
            <h2><?php esc_html_e( 'The following plugins extend GravityView and Gravity Forms functionality:', 'gravityview' ); ?></h2>
300
301
            <div class="wp-header-end"></div>
302
303
            <div class="gv-admin-installer-notice notice inline error hidden is-dismissible">
304
                <p><!-- Contents will be replaced by JavaScript if there is an error --></p>
305
            </div>
306
307
            <div class="gv-admin-installer-container">
308
				<?php
309
310
				$wp_plugins = $this->get_wp_plugins_data();
311
312
				foreach ( $downloads_data as $extension ) {
313
314
					if ( empty( $extension['info'] ) ) {
315
						continue;
316
					}
317
318
					if ( 'gravityview' === \GV\Utils::get( $extension, 'info/slug' ) ) {
319
						continue;
320
					}
321
322
					$this->render_download( $extension, $wp_plugins );
323
				}
324
				?>
325
            </div>
326
        </div>
327
		<?php
328
	}
329
330
	/**
331
	 * Outputs the HTML of a single download
332
	 *
333
	 * @param array $download Download data, as returned from EDD API
334
	 * @param array $wp_plugins
335
	 *
336
	 * @return void
337
	 */
338
	protected function render_download( $download, $wp_plugins ) {
339
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
340
341
        $details = $this->get_download_display_details( $download, $wp_plugins );
342
343
        $download_info = $details['download_info'];
344
345
		?>
346
        <div class="item <?php echo esc_attr( $details['item_class'] ); ?>">
347
            <div class="addon-inner">
348
                <a href="<?php echo esc_url( $download_info['link'] ); ?>" rel="external noreferrer noopener" title="<?php esc_html_e( 'Visit the plugin page', 'gravityview' ); ?>"><img class="thumbnail" src="<?php echo esc_attr( $download_info['thumbnail'] ); ?>" alt="" /></a>
349
                <h3><?php echo esc_html( \GV\Utils::get( $download_info, 'installer_title', $download_info['title'] ) ); ?></h3>
350
                <div>
351
                    <?php if( $details['status_label'] ) { ?>
352
                    <div class="status <?php echo esc_attr( $details['status'] ); ?>" title="<?php printf( esc_attr__( 'Plugin status: %s', 'gravityview' ), esc_html( $details['status_label'] ) ); ?>">
353
                        <span class="dashicons dashicons-admin-plugins"></span> <span class="status-label"><?php echo esc_html( $details['status_label'] ); ?></span>
354
                    </div>
355
			        <?php } ?>
356
357
                    <a data-status="<?php echo esc_attr( $details['status'] ); ?>" data-plugin-path="<?php echo esc_attr( $details['plugin_path'] ); ?>" href="<?php echo esc_url( $details['href'] ); ?>" class="button <?php echo esc_attr( $details['button_class'] ); ?>" title="<?php echo esc_attr( $details['button_title'] ); ?>">
358
                        <span class="title"><?php echo esc_html( $details['button_label'] ); ?></span>
359
                        <?php if( $details['spinner'] ) { ?><span class="spinner"></span><?php } ?>
360
                    </a>
361
                </div>
362
363
                <div class="addon-excerpt"><?php
364
365
                    $excerpt = \GV\Utils::get( $download_info, 'installer_excerpt', $download_info['excerpt'] );
366
367
                    // Allow some pure HTML tags, but remove everything else from the excerpt.
368
                    $tags = array( '<strong>', '</strong>', '<em>', '</em>', '<code>', '</code>' );
369
                    $replacements = array( '[b]', '[/b]', '[i]', '[/i]', '[code]', '[/code]' );
370
371
                    $excerpt = str_replace( $tags, $replacements, $excerpt );
372
                    $excerpt = esc_html( strip_tags( $excerpt ) );
373
					$excerpt = str_replace( $replacements, $tags, $excerpt );
374
375
					echo wpautop( $excerpt );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'wpautop'
Loading history...
376
                ?></div>
377
            </div>
378
        </div>
379
		<?php
380
	}
381
382
	/**
383
     * Generates details array for the download to keep the render_download() method a bit tidier
384
     *
385
	 * @param array $download Single download, as returned by {@see get_downloads_data}
386
	 * @param array $wp_plugins All active plugins, as returned by {@see get_plugins()}
387
	 *
388
	 * @return array {
389
     *   @type array $download_info
390
     *   @type string $plugin_path
391
     *   @type string $status License status returned by Easy Digital Downloads ("active", "inactive", "expired", "revoked", etc)
392
     *   @type string $status_label
393
     *   @type string $button_title Title attribute to show when hovering over the download's button
394
     *   @type string $button_class CSS class to use for the button
395
     *   @type string $button_label Text to use for the download's anchor link
396
     *   @type string $href URL for the download's button
397
     *   @type bool   $spinner Whether to show the spinner icon
398
     *   @type string $item_class CSS class for the download container
399
     *   @type string $required_license The name of the required license for the download ("All Access" or "Core + Extensions")
400
     *   @type bool   $is_active Is the current GravityView license (as entered in Settings) active?
401
     * }
402
	 */
403
	private function get_download_display_details( $download, $wp_plugins ) {
404
405
		$download_info = wp_parse_args( (array) $download['info'], array(
406
			'thumbnail' => '',
407
			'title' => '',
408
			'textdomain' => '',
409
			'slug' => '',
410
			'excerpt' => '',
411
			'link' => '',
412
            'coming_soon' => false,
413
			'installer_title' => null, // May not be defined
414
			'installer_excerpt' => null, // May not be defined
415
		) );
416
417
		$wp_plugin = \GV\Utils::get( $wp_plugins, $download_info['textdomain'], false );
418
419
		$has_access = ! empty( $download['files'] );
420
		$spinner = true;
421
		$href = $plugin_path = '#';
422
		$status = $item_class = $button_title = $button_class = '';
423
		$base_price = $this->get_download_base_price( $download );
424
		$is_active = in_array( gravityview()->plugin->settings->get( 'license_key_response/license' ), array( 'active', 'valid' ), true );
425
		$galactic_only = in_array( \GV\Utils::get( $download, 'info/category/0/slug' ), array( 'plugins', 'views' ) );
426
		$required_license = $galactic_only ? __( 'All Access', 'gravityview' ) : __( 'Core + Extensions', 'gravityview' );
427
428
		// The license is not active - no matter what level, this should not work
429
		if( ! $is_active  && empty( $base_price ) ) {
430
			$spinner      = false;
431
			$button_class = 'disabled disabled-license';
432
			$button_label = sprintf( __( 'Active %s License is Required.', 'gravityview' ), $required_license );
433
		}
434
435
		// No access with the current license level, and the download is available to purchase
436
		elseif ( ! $has_access && ! empty( $base_price ) ) {
437
			$spinner      = false;
438
			$status_label = '';
439
			$button_label = sprintf( __( 'Purchase Now for %s', 'gravityview' ), '$' . $base_price );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$base_price'
Loading history...
440
			$button_class = 'button-primary button-large';
441
			$href         = $download_info['link'];
442
			$item_class   = 'featured';
443
		}
444
445
		// No access with the current license level, and the download is not sold separately
446
		elseif ( ! $has_access && $is_active ) {
447
			$spinner      = false;
448
			$status_label = '';
449
			$button_label = sprintf( __( 'Upgrade to %s for Access', 'gravityview' ), $required_license );
450
			$button_class = 'button-primary button-large';
451
			$href         = 'https://gravityview.co/pricing/?utm_source=admin-installer&utm_medium=admin&utm_campaign=Admin%20Notice&utm_content=' . $required_license;
452
		}
453
454
        elseif ( ! empty( $download_info['coming_soon'] ) ) {
455
	        $spinner      = false;
456
	        $status       = 'notinstalled';
457
	        $status_label = __( 'Coming Soon', 'gravityview' );
458
	        $button_label = __( 'Learn More', 'gravityview' );
459
	        $button_class = 'button-primary button-large';
460
	        $href         = \GV\Utils::get( $download_info, 'link', 'https://gravityview.co/extensions/' );
461
        }
462
463
		// Access but the plugin is not installed
464
		elseif ( ! $wp_plugin ) {
465
466
			$href = add_query_arg(
467
				array(
468
					'action'   => 'install-plugin',
469
					'plugin'   => $download_info['slug'],
470
					'_wpnonce' => wp_create_nonce( 'install-plugin_' . $download_info['slug'] ),
471
				),
472
				self_admin_url( 'update.php' )
473
			);
474
475
			$status = 'notinstalled';
476
			$status_label = __( 'Not Installed', 'gravityview' );
477
			$button_label = __( 'Install', 'gravityview' );
478
479
		}
480
481
		// Access and the plugin is installed but not active
482
		elseif ( false === $wp_plugin['activated'] ) {
483
			$status = 'inactive';
484
			$status_label = __( 'Inactive', 'gravityview' );
485
			$button_label = __( 'Activate', 'gravityview' );
486
			$plugin_path = $wp_plugin['path'];
487
488
		}
489
490
		// Access and the plugin is installed and active
491
		else {
492
493
			$plugin_path = $wp_plugin['path'];
494
			$status = 'active';
495
			$status_label = __( 'Active', 'gravityview' );
496
			$button_label = __( 'Deactivate', 'gravityview' );
497
498
		}
499
500
		return compact( 'download_info','plugin_path', 'status', 'status_label', 'button_title', 'button_class', 'button_label', 'href', 'spinner', 'item_class', 'required_license', 'is_active' );
501
    }
502
503
	/**
504
     * Returns the base price for an extension
505
     *
506
	 * @param array $download
507
	 *
508
	 * @return float Base price for an extension. If not for sale separately, returns 0
509
	 */
510
	private function get_download_base_price( $download ) {
511
512
	    $base_price = \GV\Utils::get( $download, 'pricing/amount', 0 );
513
		$base_price = \GFCommon::to_number( $base_price );
514
515
		unset( $download['pricing']['amount'] );
516
517
		// Price options array, not single price
518
		if ( ! $base_price && ! empty( $download['pricing'] ) ) {
519
			$base_price = array_shift( $download['pricing'] );
520
		}
521
522
		return floatval( $base_price );
523
    }
524
525
	/**
526
	 * Handle AJAX request to activate extension
527
	 *
528
	 * @return void Exits with JSON response
529
	 */
530
	public function activate_download() {
531
		$data = \GV\Utils::_POST( 'data', array() );
532
533
		if ( empty( $data['path'] ) ) {
534
			return;
535
		}
536
537
		$result = activate_plugin( $data['path'] );
538
539
		if ( is_wp_error( $result ) || ! is_plugin_active( $data['path'] ) ) {
540
			wp_send_json_error( array(
541
                'error' => sprintf( __( 'Plugin activation failed: %s', 'gravityview' ), $result->get_error_message() )
542
            ) );
543
		}
544
545
		wp_send_json_success();
546
	}
547
548
	/**
549
	 * Handle AJAX request to deactivate extension
550
	 *
551
	 * @return void Send JSON response status and error message
552
	 */
553
	public function deactivate_download() {
554
		$data = \GV\Utils::_POST( 'data', array() );
555
556
		if ( empty( $data['path'] ) ) {
557
			return;
558
		}
559
560
		deactivate_plugins( $data['path'] );
561
562
		if( is_plugin_active( $data['path'] ) ) {
563
            wp_send_json_error( array(
564
                'error' => sprintf( __( 'Plugin deactivation failed.', 'gravityview' ) )
565
            ) );
566
        }
567
568
		wp_send_json_success();
569
	}
570
571
	/**
572
	 * Register and enqueue assets; localize script
573
	 *
574
	 * @return void
575
	 */
576
	public function maybe_enqueue_scripts_and_styles() {
577
578
		if ( ! gravityview()->request->is_admin( '', 'downloads' ) ) {
0 ignored issues
show
Unused Code introduced by
The call to Request::is_admin() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
579
			return;
580
		}
581
582
		$script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
583
584
		wp_enqueue_style( 'gravityview-admin-installer', GRAVITYVIEW_URL . 'assets/css/admin-installer.css', array(), \GV\Plugin::$version );
585
586
		wp_enqueue_script( 'gravityview-admin-installer', GRAVITYVIEW_URL . 'assets/js/admin-installer' . $script_debug . '.js', array( 'jquery' ), \GV\Plugin::$version, true );
587
588
		wp_localize_script( 'gravityview-admin-installer', 'gvAdminInstaller', array(
589
			'activateErrorLabel'    => __( 'Plugin activation failed.', 'gravityview' ),
590
			'deactivateErrorLabel'  => __( 'Plugin deactivation failed.', 'gravityview' ),
591
			'activeStatusLabel'     => __( 'Active', 'gravityview' ),
592
			'inactiveStatusLabel'   => __( 'Inactive', 'gravityview' ),
593
			'activateActionLabel'   => __( 'Activate', 'gravityview' ),
594
			'deactivateActionLabel' => __( 'Deactivate', 'gravityview' )
595
		) );
596
	}
597
}
598
599
new GravityView_Admin_Installer;
600