PluginProbe
WPGet API – Connect to any external REST API / 1.1.0
WPGet API – Connect to any external REST API v1.1.0
1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.10 2.2.2 2.2.3 All 97 releases
wpgetapi / includes / class-admin-options.php

class-admin-options.php in WPGet API – Connect to any external REST API 1.1.0, at includes/class-admin-options.php

481 lines 13.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings Required
4 *
5 */
6
7 // Exit if accessed directly.
8 if ( ! defined( 'ABSPATH' ) ) exit;
9
10 if ( ! class_exists( 'WpGetApi_Admin_Options' ) ) :
11
12 /**
13 * CMB2 Theme Options
14 * @version 0.1.0
15 */
16 class WpGetApi_Admin_Options {
17
18 /**
19 * Array of metaboxes/fields
20 * @var array
21 */
22 public $option_metabox = array();
23
24 /**
25 * Array of metaboxes/fields
26 * @var array
27 */
28 public $metabox_id = '';
29
30 /**
31 * Options Page title
32 * @var string
33 */
34 protected $title = '';
35
36 /**
37 * Options Page title
38 * @var string
39 */
40 protected $menu_title = '';
41
42 /**
43 * Options Tab Pages
44 * @var array
45 */
46 public $options_pages = array();
47
48 /**
49 * Holds an instance of the object
50 *
51 * @var Myprefix_Admin
52 **/
53 private static $instance = null;
54
55 /**
56 * Constructor
57 * @since 0.1.0
58 */
59 private function __construct() {
60 // Set our title
61 $this->menu_title = __( 'WPGetAPI', 'wpgetapi' );
62 $this->title = __( 'WPGetAPI', 'wpgetapi' );
63 }
64
65 /**
66 * Returns the running object
67 *
68 * @return Myprefix_Admin
69 **/
70 public static function get_instance() {
71 if( is_null( self::$instance ) ) {
72 self::$instance = new self();
73 self::$instance->hooks();
74 }
75 return self::$instance;
76 }
77
78 /**
79 * Initiate our hooks
80 * @since 0.1.0
81 */
82 public function hooks() {
83 add_action( 'admin_init', array( $this, 'init' ) );
84 add_action( 'admin_menu', array( $this, 'add_options_pages' ) );
85
86 add_action( 'cmb2_init', array( $this, 'init_custom_fields' ) );
87 }
88
89 public function init_custom_fields() {
90 require_once WPGETAPIDIR . 'includes/class-fields.php';
91 WpGetApi_Parameter_Field::init_parameter();
92 }
93
94 /**
95 * Register our setting to WP
96 * @since 0.1.0
97 */
98 public function init() {
99 $option_tabs = self::option_fields();
100 foreach ($option_tabs as $index => $option_tab) {
101 register_setting( $option_tab['id'], $option_tab['id'] );
102 }
103 }
104
105
106 /**
107 * Get our saved API's from setup
108 * @since 0.1.0
109 */
110 public function get_apis() {
111
112 $setup = get_option( 'wpgetapi_setup' );
113
114 if( empty( $setup ) || ! isset( $setup['apis'] ) || empty( $setup['apis'] ) )
115 return;
116
117 return $setup['apis'];
118
119 }
120
121
122 /**
123 * Add the options metabox to the array of metaboxes
124 * @since 0.1.0
125 */
126 public function option_fields() {
127
128 //Only need to initiate the array once per page-load
129 if ( ! empty( $this->option_metabox ) ) {
130 return $this->option_metabox;
131 }
132
133 // sale tab
134 $option_metabox[] = array(
135 'title' => __( 'Setup', 'wpgetapi' ),
136 'menu_title' => __( 'Setup', 'wpgetapi' ),
137 'id' => 'wpgetapi_setup',
138 'desc' => __( 'Add the details of your API(s) below and hit the Save button.', 'wpgetapi' ),
139 'show_on' => array( 'key' => 'options-page', 'value' => array( 'setup' ), ),
140 'show_names' => true,
141 'fields' => $this->setup_fields(),
142 );
143
144 // get our saved API's and create as tabs
145 if( $this->get_apis() ) {
146
147 foreach ( $this->get_apis() as $index => $api ) {
148
149 $name = isset( $api['name'] ) && $api['name'] != '' ? sanitize_text_field( $api['name'] ) : 'API ' . absint( $index );
150 $api_id = isset( $api['id'] ) && $api['id'] != '' ? sanitize_text_field( $api['id'] ) : $name;
151 $metabox_id = strtolower( str_replace( '-', '_', sanitize_file_name( 'wpgetapi-' . $api_id ) ) );
152 $base_url = isset( $api['base_url'] ) && $api['base_url'] != '' ? esc_url_raw( $api['base_url'], array( 'http', 'https' ) ) : '';
153 $type = isset( $api['auth_type'] ) && $api['auth_type'] != '' ? sanitize_text_field( $api['auth_type'] ) : '';
154
155 // tab
156 $option_metabox[] = array(
157 'title' => $name,
158 'menu_title' => $name,
159 'id' => $metabox_id,
160 'desc' => '',
161 'show_on' => array( 'key' => 'options-page', 'value' => array( $metabox_id ), ),
162 'show_names' => true,
163 'fields' => $this->api_fields( $type, $api_id, $base_url ),
164 );
165
166 }
167
168 }
169
170 return $option_metabox;
171
172 }
173
174
175 public function setup_fields() {
176
177 $fields[] = array(
178 'id' => 'apis',
179 'type' => 'group',
180 'name' => '',
181 'description' => '',
182 'options' => array(
183 'group_title' => __( 'API {#}', 'wpgetapi' ),
184 'add_button' => __( 'Add API', 'wpgetapi' ),
185 'remove_button' => __( 'Remove API', 'wpgetapi' ),
186 'sortable' => true, // beta
187 ),
188 'fields' => array(
189 array(
190 'name' => __( 'API Name', 'wpgetapi' ),
191 'id' => 'name',
192 'type' => 'text',
193 'attributes' => array(
194 'required' => true,
195 'placeholder' => 'New API Name',
196 ),
197 'desc' => __( 'The name of the API you are connecting to.', 'wpgetapi' ),
198 ),
199 array(
200 'name' => __( 'API ID', 'wpgetapi' ),
201 'id' => 'id',
202 'type' => 'text',
203 'attributes' => array(
204 'required' => true,
205 'placeholder' => 'new_api_name',
206 ),
207 'desc' => __( 'A unique ID for your API. Once this is set you should not change this value.', 'wpgetapi' ),
208 ),
209 array(
210 'name' => __( 'Base URL', 'wpgetapi' ),
211 'id' => 'base_url',
212 'type' => 'text',
213 'attributes' => array(
214 'required' => true,
215 'placeholder' => 'https://newapiurl.com',
216 ),
217 'desc' => __( 'The base URL of the API you are connecting to.', 'wpgetapi' ),
218 ),
219
220 )
221 );
222
223 return $fields;
224
225 }
226
227
228 /**
229 * Site wide info on the current sale.
230 * @return sale info array
231 *
232 */
233 public function api_fields( $type = '', $api_id = '', $base_url = '' ) {
234
235 $fields = array();
236
237 $fields[] = array(
238 'name' => __( 'Timeout', 'wpgetapi' ),
239 'id' => 'timeout',
240 'type' => 'text',
241 'desc' => __( 'Timeout of the API call.', 'wpgetapi' ),
242 'attributes' => array(
243 'placeholder' => '10'
244 ),
245 'before_row' => '<pre class="url"><span>Base URL: </span>' . $base_url . '</pre>',
246 );
247 $fields[] = array(
248 'name' => __( 'SSL Verify', 'wpgetapi' ),
249 'id' => 'sslverify',
250 'type' => 'select',
251 'desc' => __( 'This should normally be set to true. If you are having issues, you can try false.', 'wpgetapi' ),
252 'options' => array(
253 '1' => 'True',
254 '0' => 'False'
255 ),
256 );
257
258 $endpoint_fields = apply_filters( 'wpgetapi_fields_endpoints', array(
259 array(
260 'name' => __( 'Unique ID', 'wpgetapi' ),
261 'id' => 'id',
262 'type' => 'text',
263 'classes' => 'field-id',
264 'attributes' => array(
265 'required' => true,
266 'placeholder' => 'new_endpoint',
267 ),
268 'desc' => __( 'Choose a unique ID for this endpoint. Once this is set you should not change this value.', 'wpgetapi' ),
269 'before_row' => "
270 <pre class='functions'>
271 Template Function: </span><span>wpgetapi_endpoint( '" . $api_id . "', '<span class='endpoint_id'></span>', array('debug' => false) );</span><br>
272 Shortcode: <span>[wpgetapi_endpoint api_id='" . $api_id . "' endpoint_id='<span class='endpoint_id'></span>' debug='false']</span>
273 </pre>
274 ",
275 ),
276 array(
277 'name' => __( 'Endpoint', 'wpgetapi' ),
278 'id' => 'endpoint',
279 'type' => 'text',
280 'classes' => 'field-endpoint',
281 'attributes' => array(
282 'required' => true,
283 'placeholder' => '/newendpoint',
284 ),
285 'desc' => __( 'The endpoint that will be appended to the base URL to get the data. Include slash at beginning.', 'wpgetapi' ),
286 ),
287 array(
288 'name' => __( 'Method', 'wpgetapi' ),
289 'id' => 'method',
290 'type' => 'select',
291 'classes' => 'field-method',
292 'attributes' => array(
293 'required' => true,
294 ),
295 'options' => array(
296 'GET' => 'GET',
297 'POST' => 'POST',
298 ),
299 'desc' => __( 'The request method for this endpoint.', 'wpgetapi' ),
300 ),
301 array(
302 'name' => __( 'Results Format', 'wpgetapi' ),
303 'id' => 'results_format',
304 'type' => 'select',
305 'classes' => 'field-results-format',
306 'attributes' => array(
307 'required' => true,
308 ),
309 'options_cb' => 'wpgetapi_results_format_options',
310 'desc' => __( 'The format of the results.', 'wpgetapi' ),
311 ),
312 array(
313 'name' => __( 'Query String', 'wpgetapi' ),
314 'id' => 'query_parameters',
315 'type' => 'parameter',
316 'classes' => 'field-query-parameters',
317 'repeatable' => true,
318 'desc' => __( 'Parameters as name/value pairs that will be appended to the URL. ', 'wpgetapi' ),
319 ),
320 array(
321 'name' => __( 'Headers', 'wpgetapi' ),
322 'id' => 'header_parameters',
323 'type' => 'parameter',
324 'classes' => 'field-header-parameters',
325 'repeatable' => true,
326 'desc' => __( 'Parameters as name/value pairs that will be included in the header. ', 'wpgetapi' ),
327 ),
328 array(
329 'name' => __( 'Body', 'wpgetapi' ),
330 'id' => 'body_parameters',
331 'type' => 'parameter',
332 'classes' => 'field-body-parameters',
333 'repeatable' => true,
334 'desc' => __( 'Parameters as name/value pairs that will be included in the POST body. Only used when POST method is used and will be JSON encoded. ', 'wpgetapi' ),
335 ),
336 ) );
337
338
339 $fields[] = array(
340 'id' => 'endpoints',
341 'type' => 'group',
342 'name' => __( 'Endpoints', 'wpgetapi' ),
343 'description' => '',
344 'options' => array(
345 'group_title' => __( 'Endpoint {#}', 'wpgetapi' ),
346 'add_button' => __( 'Add Endpoint', 'wpgetapi' ),
347 'remove_button' => __( 'Remove Endpoint', 'wpgetapi' ),
348 'sortable' => true, // beta
349 ),
350 'fields' => $endpoint_fields,
351 );
352
353 return $fields;
354
355 }
356
357
358 public function add_options_pages() {
359
360 $option_tabs = self::option_fields();
361
362 foreach ($option_tabs as $index => $option_tab) {
363 if ( $index == 0) {
364
365 $this->options_pages[] = add_menu_page( $this->title, $this->menu_title, 'manage_options', $option_tab['id'], array( $this, 'admin_page_display' ), 'dashicons-editor-code'
366 ); //Link admin menu to first tab
367
368 add_submenu_page( $option_tabs[0]['id'], $this->menu_title, $option_tab['menu_title'], 'manage_options', $option_tab['id'], array( $this, 'admin_page_display' ) ); //Duplicate menu link for first submenu page
369 } else {
370 $this->options_pages[] = add_submenu_page( $option_tabs[0]['id'], $this->menu_title, $option_tab['menu_title'], 'manage_options', $option_tab['id'], array( $this, 'admin_page_display' ) );
371 }
372 }
373
374 foreach ( $this->options_pages as $page ) {
375 // Include CMB CSS in the head to avoid FOUC
376 add_action( "admin_print_styles-{$page}", array( 'CMB2_Hookup', 'enqueue_cmb_css' ) );
377 }
378
379 }
380
381 /**
382 * Admin page markup. Mmply handled by CMB2
383 * @since 0.1.0
384 */
385 public function admin_page_display() {
386
387 $option_tabs = self::option_fields(); //get all option tabs
388 $tab_forms = array();
389 ?>
390
391 <div class="wrap wpgetapi">
392
393 <div class="main_content_cell">
394
395 <h2><?php esc_html_e( $this->title, 'wpgetapi' ) ?></h2>
396 <!-- Options Page Nav Tabs -->
397 <h2 class="nav-tab-wrapper">
398 <?php foreach ($option_tabs as $option_tab) :
399 $tab_slug = $option_tab['id'];
400 $nav_class = 'nav-tab';
401 if ( $tab_slug == $_GET['page'] ) {
402 $nav_class .= ' nav-tab-active'; //add active class to current tab
403 $tab_forms[] = $option_tab; //add current tab to forms to be rendered
404 }
405 ?>
406 <a class="<?php esc_attr_e( $nav_class ); ?>" href="<?php esc_url( menu_page_url( $tab_slug ) ); ?>"><?php esc_attr_e( $option_tab['title'], 'wpgetapi' ); ?></a>
407 <?php endforeach; ?>
408 </h2>
409 <!-- End of Nav Tabs -->
410
411 <?php foreach ($tab_forms as $tab_form) : //render all tab forms (normaly just 1 form) ?>
412
413 <div id="<?php esc_attr_e($tab_form['id']); ?>" class="cmb-form group">
414 <div class="metabox-holder">
415 <div class="pmpbox pad">
416 <h3 class="title"><?php esc_html_e($tab_form['title'], 'wpgetapi'); ?></h3>
417 <div class="desc"><?php echo wp_kses_post( $tab_form['desc'] ); ?></div>
418 <?php cmb2_metabox_form( $tab_form, $tab_form['id'] ); ?>
419 </div>
420 </div>
421 </div>
422
423 <?php endforeach; ?>
424
425 </div>
426
427 <div class="sidebar_cell">
428 <div class="box">
429 <h3>Help & Tips</h3>
430 <p><a target="_blank" href="https://wpgetapi.com/docs">View The Docs</a></p>
431 <p>Using the Template Function within your theme is the most flexible option. It does require basic knowledge of PHP.</p>
432 <p>Trying to 'echo' the data when using the 'JSON (as array data)' option will result in a PHP warning. You will need to use 'var_dump' instead to see the output.</p>
433 <p>Using the Shortcode within a page (or post) becomes more flexible when used in conjunction with our <a target="_blank" href="https://wpgetapi.com/downloads/pro-plugin">WPGetAPI Pro</a> plugin extension.</p>
434
435 <p>You can easily get nested data from the array using our <a target="_blank" href="https://wpgetapi.com/downloads/pro-plugin">WPGetAPI Pro</a> plugin extension using either Shortcode or Template Function.</p>
436
437 </div>
438 <div class="box">
439 <h3>Contact Us</h3>
440 <p>Please <a target="_blank" href="https://wpgetapi.com/">visit our website</a> for any questions, help or feature requests. We are more than happy to help you set up your API.</p>
441 </div>
442 </div>
443
444 </div>
445
446 <?php
447
448 }
449
450 /**
451 * Public getter method for retrieving protected/private variables
452 * @since 0.1.0
453 * @param string $field Field to retrieve
454 * @return mixed Field value or exception is thrown
455 */
456 public function __get( $field ) {
457 // Allowed fields to retrieve
458 if ( in_array( $field, array( 'key', 'fields', 'title', 'options_pages' ), true ) ) {
459 return $this->{$field};
460 }
461 if ( 'option_metabox' === $field ) {
462 return $this->option_fields();
463 }
464 throw new Exception( 'Invalid property: ' . $field );
465 }
466
467 }
468
469 /**
470 * Helper function to get/return the WpGetApi_Admin_Options object
471 * @since 0.1.0
472 * @return WpGetApi_Admin_Options object
473 */
474 function wpgetapi_admin_options() {
475 return WpGetApi_Admin_Options::get_instance();
476 }
477
478 // Get it started
479 wpgetapi_admin_options();
480
481 endif;