Completed
Push — release/2.0 ( 4d845f...d9fa8a )
by Ravinder
18:24
created

export-functions.php ➔ give_do_ajax_export()   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 83
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 44
nc 8
nop 0
dl 0
loc 83
rs 5.48
c 0
b 0
f 0

How to fix   Long Method   

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

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
 * Exports Functions
4
 *
5
 * These are functions are used for exporting data from Give
6
 *
7
 * @package     Give
8
 * @subpackage  Admin/Export
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
19
20
/**
21
 * Process batch exports via ajax
22
 *
23
 * @since 1.5
24
 * @return void
25
 */
26
function give_do_ajax_export() {
27
28
	require_once GIVE_PLUGIN_DIR . 'includes/admin/tools/export/class-batch-export.php';
29
30
	parse_str( $_POST['form'], $form );
31
32
	$_REQUEST = $form = (array) $form;
33
34
	if ( ! wp_verify_nonce( $_REQUEST['give_ajax_export'], 'give_ajax_export' ) ) {
35
		die( '-2' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_do_ajax_export() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
36
	}
37
38
	/**
39
	 * Fires before batch export.
40
	 *
41
	 * @since 1.5
42
	 *
43
	 * @param string $class Export class.
44
	 */
45
	do_action( 'give_batch_export_class_include', $form['give-export-class'] );
46
47
	$step   = absint( $_POST['step'] );
48
	$class  = sanitize_text_field( $form['give-export-class'] );
49
50
	$export = new $class( $step );
51
52
	if ( ! $export->can_export() ) {
53
		die( '-1' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_do_ajax_export() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
54
	}
55
56
	if ( ! $export->is_writable ) {
57
		$json_args = array(
58
			'error'   => true,
59
			'message' => esc_html__( 'Export location or file not writable.', 'give' )
60
		);
61
		echo json_encode($json_args);
62
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_do_ajax_export() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
63
	}
64
65
	$export->set_properties( $_REQUEST );
66
67
	$export->pre_fetch();
68
69
	$ret = $export->process_step( $step );
70
71
	$percentage = $export->get_percentage_complete();
72
73
	if ( $ret ) {
74
75
		$step += 1;
76
		echo json_encode( array( 'step' => $step, 'percentage' => $percentage ) );
77
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_do_ajax_export() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
78
79
	} elseif ( true === $export->is_empty ) {
80
81
		echo json_encode( array(
82
			'error'   => true,
83
			'message' => esc_html__( 'No data found for export parameters.', 'give' )
84
		) );
85
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_do_ajax_export() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
86
87
	} elseif ( true === $export->done && true === $export->is_void ) {
88
89
		$message = ! empty( $export->message ) ? $export->message : esc_html__( 'Batch Processing Complete', 'give' );
90
		echo json_encode( array( 'success' => true, 'message' => $message ) );
91
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_do_ajax_export() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
92
93
	} else {
94
		
95
		$args = array_merge( $_REQUEST, array(
96
			'step'        => $step,
97
			'class'       => $class,
98
			'nonce'       => wp_create_nonce( 'give-batch-export' ),
99
			'give_action' => 'form_batch_export',
100
		) );
101
102
		$download_url = add_query_arg( $args, admin_url() );
103
104
		echo json_encode( array( 'step' => 'done', 'url' => $download_url ) );
105
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_do_ajax_export() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
106
107
	}
108
}
109
110
add_action( 'wp_ajax_give_do_ajax_export', 'give_do_ajax_export' );
111