AdminPage::hooks()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 1 Features 3
Metric Value
cc 1
eloc 7
c 10
b 1
f 3
nc 1
nop 0
dl 0
loc 11
rs 9.4285
1
<?php
2
namespace testContent;
3
4
/**
5
 * Class to build test data for custom post types.
6
 *
7
 * @package    WordPress
8
 * @subpackage Evans
9
 * @author     Old Town Media
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 Test Content', 'otm-test-content' ),
84
			__( 'Test Content', 'otm-test-content' ),
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">' . __( 'Build Test Content', 'otm-test-content' ) . '</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>'.__( '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.', 'otm-test-content' ).'</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
			'createdStr'	=> __( 'Created', 'otm-test-content' ),
151
			'deletedStr'	=> __( 'Deleting', 'otm-test-content' ),
152
			'creatingStr'	=> __( 'Creating', 'otm-test-content' ),
153
		);
154
155
		wp_localize_script( 'test-content-js', 'test_content', $data );
156
157
	}
158
159
160
	/**
161
	 * Print out our admin page to control test data.
162
	 */
163
	public function admin_page(){
164
		echo '<div class="wrap" id="options_editor">' . "\n";
165
166
			echo '<h2>' . __( 'Create Test Data' , 'otm-test-content' ) . '</h2>' . "\n";
167
168
			echo "<div class='nav-tab-wrapper'>";
169
170
				do_action( 'tc-admin-tabs', '' );
171
172
			echo "</div>";
173
174
			echo "";
175
176
				do_action( 'tc-admin-sections', '' );
177
178
			echo "";
179
180
			echo "<input type='hidden' id='connection-status' value='".$this->connected."'>";
181
182
			echo "<pre class='test-data-status-box' id='status-updates'></pre>";
183
184
		echo "</div>";
185
186
	}
187
188
}
189