Ajax::hooks()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace DummyPress;
3
4
/**
5
 * Handling Ajax return and data
6
 *
7
 * @package    WordPress
8
 * @subpackage Evans
9
 * @author     Mike Selander
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
	/**
23
	 * plugin
24
	 * Plugin class instance.
25
	 *
26
	 * @var object
27
	 */
28
	private $plugin;
29
30
	/**
31
	 * action
32
	 * Name of the action we want to use in our AJAX calls
33
	 *
34
	 * @var string
35
	 */
36
	private $action;
37
38
39
	/**
40
	 * Instantiate any WP hooks that need to be fired.
41
	 */
42
	public function hooks() {
43
44
		$this->reporting    = new Reporting;
45
		$this->action       = 'handle_test_data';
46
47
		add_action( "wp_ajax_{$this->action}", array( $this, 'handle_ajax' ) );
48
		add_filter( 'option_active_plugins', array( $this, 'ajax_exclude_plugins' ) );
49
50
	}
51
52
53
	/**
54
	 * Set a reference to the main plugin instance.
55
	 *
56
	 * @param $plugin Plugin instance.
57
	 * @return Ajax instance
58
	 */
59
	public function set_plugin( $plugin ) {
60
61
		$this->plugin = $plugin;
62
		return $this;
63
64
	}
65
66
67
	/**
68
	 * Turn outside plugins off during our AJAX calls to speed everything up.
69
	 *
70
	 * Having a lot of plugins running slows down an AJAX request, this function
71
	 * turns all other plugins off temporarliy while the AJAX requests is running.
72
	 *
73
	 * https://deliciousbrains.com/excluding-wordpress-plugins-loading-specific-ajax-requests/
74
	 *
75
	 * @param array $plugins All active plugins.
76
	 * @return array Whitelisted plugins.
77
	 */
78
	public function ajax_exclude_plugins( $plugins ) {
0 ignored issues
show
Coding Style introduced by
ajax_exclude_plugins uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
79
80
		if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || ! isset( $_POST['action'] ) || false === strpos( $_POST['action'], $this->action ) ) {
81
			return $plugins;
82
		}
83
84
		foreach( $plugins as $key => $plugin ) {
85
86
			if ( false !== strpos( $plugin, $this->plugin->definitions->slug ) ) {
87
				continue;
88
			}
89
90
			unset( $plugins[$key] );
91
		}
92
93
		return $plugins;
94
95
	}
96
97
98
	/**
99
	 * Ajax callback function for triggering the creation & deletion of test data.
100
	 *
101
	 * @see wp_ajax filter, $this->add_menu_item, $this->creation_routing
102
	 */
103
	public function handle_ajax() {
0 ignored issues
show
Coding Style introduced by
handle_ajax uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
104
105
		$action		= $_REQUEST['todo'];
106
		$nonce		= $_REQUEST['nonce'];
107
108
		// Verify that we have a proper logged in user and it's the right person
109
		if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'handle-test-data' ) ) {
110
			return;
111
		}
112
113
		if ( $action == 'delete' ) {
114
115
			$this->deletion_routing( $_REQUEST );
116
117
		} elseif ( $action == 'create' ) {
118
119
			$this->creation_routing( $_REQUEST );
120
121
		}
122
123
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle_ajax() 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...
124
125
	}
126
127
128
	/**
129
	 * Choose which type of creation needs to be accomplished and route through
130
	 * the correct class.
131
	 */
132
	private function creation_routing( $data ) {
133
134
		$type = 'DummyPress\Types\\' . ucwords( $data['type'] );
135
		$object = new $type();
136
		$return = $object->create_objects( $data['slug'], $data['connection'], true, 1 );
137
138
		$clean = $this->reporting->create_report( $return );
139
140
		echo $clean;
141
142
	}
143
144
145
	/**
146
	 * Choose which type of deletion needs to be accomplished and route through
147
	 * the correct method of Delete.
148
	 */
149
	private function deletion_routing( $data ) {
150
151
		$delete_content = new Delete;
152
153
		if ( $data['type'] == 'all' ) {
154
155
			$return = $delete_content->delete_all_test_data();
156
157
		} else {
158
159
			$return = $delete_content->delete_objects( $data );
160
161
		}
162
163
		$clean = $this->reporting->create_report( $return );
164
165
		echo $clean;
166
167
	}
168
169
170
}
171