AdminPage   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 180
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hooks() 0 11 1
A set_plugin() 0 6 1
A add_settings_link() 0 7 1
A internet_connected_admin_notice() 0 22 3
A load_scripts() 0 17 1
B admin_page() 0 24 1
A add_menu_item() 0 14 1
1
<?php
2
namespace DummyPress;
3
4
/**
5
 * Class to build test data for custom post types.
6
 *
7
 * @package    WordPress
8
 * @subpackage Evans
9
 * @author     Mike Selander
10
 */
11
class AdminPage {
12
13
	/**
14
	 * plugin
15
	 * Access to plugin definitions.
16
	 *
17
	 * @var Plugin
18
	 * @access private
19
	 */
20
	private $plugin;
21
22
	/**
23
	 * definitions
24
	 * Easy way to access all of our defined paths & info.
25
	 *
26
	 * @var object
27
	 * @access private
28
	 */
29
	private $definitions;
30
31
	/**
32
	 * connected
33
	 * Whether or not we're successfully connected to the Internet.
34
	 *
35
	 * @var boolean
36
	 * @access private
37
	 */
38
	private $connected;
39
40
41
	/**
42
	 * Hooks function.
43
	 *
44
	 * This function is used to avoid loading any unnecessary functions/code.
45
	 *
46
	 * @see admin_menu, wp_ajax actions
47
	 */
48
	public function hooks() {
49
50
		$connection = new ConnectionTest;
51
		$this->definitions	= $this->plugin->get_definitions();
52
		$this->connected	= $connection->test();
53
54
		add_action( 'admin_menu', array( $this, 'add_menu_item' ) );
55
		add_filter( 'plugin_action_links_' . $this->definitions->basename, array( $this, 'add_settings_link' ) );
56
		add_action( 'admin_notices', array( $this, 'internet_connected_admin_notice' ) );
57
58
	}
59
60
61
	/**
62
	 * Set a reference to the main plugin instance.
63
	 *
64
	 * @param Plugin $plugin Main plugin instance.
65
	 */
66
	public function set_plugin( $plugin ) {
67
68
		$this->plugin = $plugin;
69
		return $this;
70
71
	}
72
73
74
	/**
75
	 * Add the admin-side menu item for creating & deleting test data.
76
	 *
77
	 * @see add_submenu_page
78
	 */
79
	public function add_menu_item() {
80
81
		$page = add_submenu_page(
82
			'tools.php',
83
			__( 'Create Dummy Content', 'dummybot' ),
84
			__( 'DummyBot', 'dummybot' ),
85
			'manage_options',
86
			'create-test-data',
87
			array( $this, 'admin_page' )
88
		);
89
90
		add_action( 'admin_print_styles-' . $page, array( $this, 'load_scripts' ) );
91
92
	}
93
94
	/**
95
	 * Add 'build test content' link to plugin list table.
96
	 *
97
	 * @param  array $links Existing links
98
	 * @return array 		Modified links
99
	 */
100
	public function add_settings_link( $links ) {
101
102
		$settings_link = '<a href="tools.php?page=create-test-data">' . esc_html__( 'Build Test Content', 'dummybot' ) . '</a>';
103
  		array_push( $links, $settings_link );
104
  		return $links;
105
106
	}
107
108
109
	/**
110
	 * Admin notice to notify a user that images won't work on test dat.
111
	 *
112
	 * Adds an admin notice that first checks if a user is connected to the
113
	 * Internet, and the test fails displays a notice informing the user that
114
	 * images will not pull into test data.
115
	 */
116
	public function internet_connected_admin_notice() {
117
118
		// Get the current admin screen & verify that we're on the right one
119
		// before continuing.
120
		$screen = get_current_screen();
121
122
		if ( $screen->base != 'tools_page_create-test-data' ) {
123
			return;
124
		}
125
126
		// Check the response
127
		if ( $this->connected ) {
128
			// We got a response so early return
129
			return;
130
		} else {
131
			// We didn't get a reponse so print the notice out
132
			echo '<div class="notice notice-error">';
133
		        echo '<p>' . esc_html__( 'WordPress could not connect to Splashbase and therefore images will not pull into metaboxes/thumbnails. Turn Airplane Mode off or reconnect to the Internet to get images when creating test data.', 'dummybot' ) . '</p>';
134
		    echo '</div>';
135
		}
136
137
	}
138
139
140
	/**
141
	 * Load our script in the admin section and serve in data.
142
	 */
143
	public function load_scripts() {
144
145
		wp_enqueue_script( 'test-content-js', plugins_url( 'assets/admin.js' , dirname( __FILE__ ) ) );
146
		wp_enqueue_style( 'test-content-css', plugins_url( 'assets/admin.css' , dirname( __FILE__ ) ) );
147
148
		$data = array(
149
			'nonce'		=> wp_create_nonce( 'handle-test-data' ),
150
			'strings'	=> array(
151
				'createdStr'	=> __( 'Created', 'dummybot' ),
152
				'deletedStr'	=> __( 'Deleting', 'dummybot' ),
153
				'creatingStr'	=> __( 'Creating', 'dummybot' ),
154
			),
155
		);
156
157
		wp_localize_script( 'test-content-js', 'test_content', $data );
158
159
	}
160
161
162
	/**
163
	 * Print out our admin page to control test data.
164
	 */
165
	public function admin_page() {
166
		echo '<div class="wrap" id="options_editor">' . "\n";
167
168
			echo '<h2>' . esc_html__( 'Create Test Data', 'dummybot' ) . '</h2>' . "\n";
169
170
			echo "<div class='nav-tab-wrapper'>";
171
172
				do_action( 'tc-admin-tabs', '' );
173
174
			echo "</div>";
175
176
			echo "";
177
178
				do_action( 'tc-admin-sections', '' );
179
180
			echo "";
181
182
			echo "<input type='hidden' id='connection-status' value='" . esc_attr( $this->connected ) . "'>";
183
184
			echo "<pre class='test-data-status-box' id='status-updates'></pre>";
185
186
		echo "</div>";
187
188
	}
189
190
}
191