Completed
Push — master ( add22c...fb4c0b )
by Mike
02:33
created

Ajax::handle_test_data_callback()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 10
c 1
b 1
f 0
nc 4
nop 0
dl 0
loc 23
rs 8.5906
1
<?php
2
namespace testContent;
3
4
/**
5
 * Handling Ajax return and data
6
 *
7
 * @package    WordPress
8
 * @subpackage Evans
9
 * @author     Old Town Media
10
 */
11
class Ajax{
12
13
	/**
14
	 * reporting
15
	 * Reporting class instance.
16
	 *
17
	 * @var object
18
	 * @access private
19
	 */
20
	private $reporting;
21
22
	public function hooks(){
23
24
		$this->reporting = new Reporting;
25
		add_action( 'wp_ajax_handle_test_data', array( $this, 'handle_test_data_callback' ) );
26
27
	}
28
29
	/**
30
	 * Ajax callback function for triggering the creation & deletion of test data.
31
	 *
32
	 * @see wp_ajax filter, $this->add_menu_item, $this->creation_routing
33
	 */
34
	public function handle_test_data_callback() {
35
36
		$action		= $_REQUEST['todo'];
37
		$nonce		= $_REQUEST['nonce'];
38
39
		// Verify that we have a proper logged in user and it's the right person
40
		if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'handle-test-data' ) ){
41
			return;
42
		}
43
44
		if ( $action == 'delete' ){
45
46
			$this->deletion_routing( $_REQUEST );
47
48
		} elseif ( $action == 'create' ){
49
50
			$this->creation_routing( $_REQUEST );
51
52
		}
53
54
		die();
55
56
	}
57
58
59
	/**
60
	 * Choose which type of creation needs to be accomplished and route through
61
	 * the correct class.
62
	 */
63
	private function creation_routing( $data ){
64
65
		$type = 'testContent\Types\\' . ucwords( $data['type'] );
66
		$object = new $type();
67
		$return = $object->create_objects( $data['slug'], $data['connection'], true, 1 );
68
69
		$clean = $this->reporting->create_report( $return );
70
71
		echo $clean;
72
73
	}
74
75
76
	/**
77
	 * Choose which type of deletion needs to be accomplished and route through
78
	 * the correct method of Delete.
79
	 */
80
	private function deletion_routing( $data ){
81
82
		$delete_content = new Delete;
83
84
		if ( $data['type'] == 'all' ){
85
86
			$return = $delete_content->delete_all_test_data();
87
88
		} else {
89
90
			$return = $delete_content->delete_objects( $data );
91
92
		}
93
94
		$clean = $this->reporting->create_report( $return );
95
96
		echo $clean;
97
98
	}
99
100
101
}
102