PluginProbe
WPGet API – Connect to any external REST API / 1.7.0
WPGet API – Connect to any external REST API v1.7.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.7.0, at includes/class-admin-options.php

746 lines 22.2 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 * Is pro plugin installed
50 * @var array
51 */
52 public $pro_plugin = false;
53
54 /**
55 * Holds an instance of the object
56 *
57 * @var Myprefix_Admin
58 **/
59 private static $instance = null;
60
61 /**
62 * Constructor
63 * @since 0.1.0
64 */
65 private function __construct() {
66 // Set our title
67 $this->menu_title = __( 'WPGetAPI', 'wpgetapi' );
68 $this->title = __( 'WPGetAPI', 'wpgetapi' );
69
70 }
71
72 /**
73 * Returns the running object
74 *
75 * @return Myprefix_Admin
76 **/
77 public static function get_instance() {
78 if( is_null( self::$instance ) ) {
79 self::$instance = new self();
80 self::$instance->hooks();
81 }
82 return self::$instance;
83 }
84
85 /**
86 * Initiate our hooks
87 * @since 0.1.0
88 */
89 public function hooks() {
90 add_action( 'admin_init', array( $this, 'init' ) );
91 add_action( 'admin_menu', array( $this, 'add_options_pages' ) );
92
93 add_action( 'cmb2_admin_init', array( $this, 'init_custom_fields' ) );
94
95 add_action( "cmb2_save_options-page_fields", array( $this, 'redirect' ), 1, 2 );
96
97 add_action( 'admin_footer', array( $this, 'load_testing_javascript' ) );
98
99 add_action( 'wp_ajax_wpgetapi_test_endpoint', array( $this, 'test_the_endpoint' ) );
100
101 }
102
103
104 /**
105 * Setup our custom fields
106 * @since 0.1.0
107 */
108 public function init_custom_fields() {
109 require_once WPGETAPIDIR . 'includes/class-fields.php';
110 WpGetApi_Parameter_Field::init_parameter();
111 }
112
113 /**
114 * Register our setting to WP
115 * @since 0.1.0
116 */
117 public function init() {
118 $this->pro_plugin = is_plugin_active( 'wpgetapi-extras/wpgetapi-extras.php' ) ? true : false;
119 $option_tabs = self::option_fields();
120 foreach ($option_tabs as $index => $option_tab) {
121 register_setting( $option_tab['id'], $option_tab['id'] );
122 }
123 }
124
125
126 /**
127 * Get our saved API's from setup
128 * @since 0.1.0
129 */
130 public function get_apis() {
131 $setup = get_option( 'wpgetapi_setup' );
132 if( empty( $setup ) || ! isset( $setup['apis'] ) || empty( $setup['apis'] ) )
133 return;
134 return $setup['apis'];
135 }
136
137
138 /**
139 * Add the options metabox to the array of metaboxes
140 * @since 0.1.0
141 */
142 public function option_fields() {
143
144 //Only need to initiate the array once per page-load
145 if ( ! empty( $this->option_metabox ) ) {
146 return $this->option_metabox;
147 }
148
149 // setup tab
150 $option_metabox[] = array(
151 'title' => __( 'Setup', 'wpgetapi' ),
152 'menu_title' => __( 'Setup', 'wpgetapi' ),
153 'id' => 'wpgetapi_setup',
154 'desc' => __( 'Add the details of the API(s) you are using. Endpoints will be setup in the next step after saving your API(s).', 'wpgetapi' ),
155 'show_on' => array( 'key' => 'options-page', 'value' => array( 'wpgetapi_setup' ), ),
156 'show_names' => true,
157 'fields' => $this->setup_fields(),
158 );
159
160 // get our saved API's and create as tabs
161 if( $this->get_apis() ) {
162
163 foreach ( $this->get_apis() as $index => $api ) {
164
165 $name = isset( $api['name'] ) && $api['name'] != '' ? sanitize_text_field( $api['name'] ) : 'API ' . absint( $index );
166 $api_id = isset( $api['id'] ) && $api['id'] != '' ? sanitize_text_field( $api['id'] ) : $name;
167 $metabox_id = strtolower( str_replace( '-', '_', sanitize_file_name( 'wpgetapi-' . $api_id ) ) );
168 $base_url = isset( $api['base_url'] ) && $api['base_url'] != '' ? esc_url_raw( $api['base_url'], array( 'http', 'https' ) ) : '';
169 $type = isset( $api['auth_type'] ) && $api['auth_type'] != '' ? sanitize_text_field( $api['auth_type'] ) : '';
170
171 // tab
172 $option_metabox[] = array(
173 'title' => $name,
174 'menu_title' => $name,
175 'id' => $metabox_id,
176 'desc' => '',
177 'show_on' => array( 'key' => 'options-page', 'value' => array( $metabox_id ), ),
178 'show_names' => true,
179 'fields' => $this->endpoint_fields( $type, $api_id, $base_url ),
180 );
181
182 }
183
184 }
185
186 $option_metabox = apply_filters( 'wpgetapi_admin_pages', $option_metabox );
187
188 return $option_metabox;
189
190 }
191
192 public function setup_fields() {
193
194 $fields[] = array(
195 'id' => 'apis',
196 'type' => 'group',
197 'name' => '',
198 'description' => '',
199 'options' => array(
200 'group_title' => __( 'API {#}', 'wpgetapi' ),
201 'add_button' => __( 'Add New API', 'wpgetapi' ),
202 'remove_button' => __( 'Remove API', 'wpgetapi' ),
203 'sortable' => true, // beta
204 ),
205 'fields' => array(
206 array(
207 'name' => __( 'API Name', 'wpgetapi' ),
208 'id' => 'name',
209 'type' => 'text',
210 'attributes' => array(
211 'required' => true,
212 'placeholder' => 'Google Maps',
213 ),
214 'desc' => __( 'The name of the API you are connecting to.', 'wpgetapi' ),
215 ),
216 array(
217 'name' => __( 'Unique ID', 'wpgetapi' ),
218 'id' => 'id',
219 'type' => 'text',
220 'attributes' => array(
221 'required' => true,
222 'placeholder' => 'google_maps',
223 ),
224 'desc' => __( 'A unique ID for your API. Lowercase letters and underscores only.', 'wpgetapi' ),
225 ),
226 array(
227 'name' => __( 'Base URL', 'wpgetapi' ),
228 'id' => 'base_url',
229 'type' => 'text',
230 'attributes' => array(
231 'required' => true,
232 'placeholder' => 'https://maps.googleapis.com/maps/api',
233 ),
234 'desc' => __( 'The base URL of the API you are connecting to.', 'wpgetapi' ),
235 ),
236
237 )
238 );
239
240 return $fields;
241
242 }
243
244
245 /**
246 * Endpoint settings.
247 * @return sale info array
248 *
249 */
250 public function endpoint_fields( $type = '', $api_id = '', $base_url = '' ) {
251
252 $fields = array();
253
254 $cache_disabled = $this->pro_plugin ? '' : 'disabled';
255 $cache_url = $this->pro_plugin ? '<a target="_blank" href="https://wpgetapi.com/downloads/pro-plugin/?utm_campaign=Cache Pro Plugin&utm_medium=Admin&utm_source=User">Pro <span class="dashicons dashicons-external"></span></a>' : '<a target="_blank" href="https://wpgetapi.com/docs/caching-api-calls/">Help <span class="dashicons dashicons-external"></span></a>';
256
257 $endpoint_fields = apply_filters( 'wpgetapi_fields_endpoints', array(
258 array(
259 'name' => __( 'Unique ID', 'wpgetapi' ),
260 'id' => 'id',
261 'type' => 'text',
262 'classes' => 'field-id',
263 'attributes' => array(
264 'required' => true,
265 'placeholder' => 'new_endpoint',
266 ),
267 'desc' => __( 'A unique ID for this endpoint. Lowercase letters and underscores only.', 'wpgetapi' ),
268 'before_row' => "wpgetapi_output_top_of_endpoint",
269 'api_id' => $api_id,
270
271 ),
272 array(
273 'name' => __( 'Endpoint', 'wpgetapi' ),
274 'id' => 'endpoint',
275 'type' => 'text',
276 'classes' => 'field-endpoint',
277 'attributes' => array(
278 'required' => true,
279 'placeholder' => '/newendpoint',
280 ),
281 'desc' => __( 'The endpoint that will be appended to the base URL.', 'wpgetapi' ),
282 ),
283 array(
284 'name' => __( 'Method', 'wpgetapi' ),
285 'id' => 'method',
286 'type' => 'select',
287 'classes' => 'field-method',
288 'attributes' => array(
289 'required' => true,
290 ),
291 'options' => array(
292 'GET' => 'GET',
293 'POST' => 'POST',
294 'PUT' => 'PUT',
295 ),
296 'desc' => __( 'The request method for this endpoint.', 'wpgetapi' ),
297 ),
298
299 array(
300 'name' => __( 'Cache Time', 'wpgetapi' ),
301 'id' => 'cache_time',
302 'type' => 'text',
303 'classes' => 'field-cache-time',
304 'attributes' => array(
305 'placeholder' => '',
306 $cache_disabled => $cache_disabled,
307 ),
308 'desc' => sprintf(
309 __( 'How many seconds to cache this request for (when set to GET method). %1s', 'wpgetapi' ),
310 $cache_url
311 )
312 ),
313
314 array(
315 'name' => __( 'Results Format', 'wpgetapi' ),
316 'id' => 'results_format',
317 'type' => 'select',
318 'classes' => 'field-results-format',
319 'attributes' => array(
320 'required' => true,
321 ),
322 'options_cb' => 'wpgetapi_results_format_options',
323 'desc' => __( 'The format of the results.', 'wpgetapi' ),
324 ),
325
326 array(
327 'name' => __( 'Query String', 'wpgetapi' ),
328 'id' => 'query_parameters',
329 'type' => 'parameter',
330 'classes' => 'field-query-parameters',
331 'repeatable' => true,
332 'desc' => sprintf(
333 __( 'Parameters as name/value pairs that will be appended to the URL. %1s', 'wpgetapi' ),
334 '<a target="_blank" href="https://wpgetapi.com/docs/adding-query-string-parameters/?utm_campaign=Query String&utm_medium=Admin&utm_source=User">Help <span class="dashicons dashicons-external"></span></a>'
335 )
336 ),
337 array(
338 'name' => __( 'Headers', 'wpgetapi' ),
339 'id' => 'header_parameters',
340 'type' => 'parameter',
341 'classes' => 'field-header-parameters',
342 'repeatable' => true,
343 'desc' => sprintf(
344 __( 'Parameters as name/value pairs, sent in the headers. %1s', 'wpgetapi' ),
345 '<a target="_blank" href="https://wpgetapi.com/docs/sending-headers-in-request/?utm_campaign=Headers&utm_medium=Admin&utm_source=User">Help <span class="dashicons dashicons-external"></span></a>'
346 )
347 ),
348 array(
349 'name' => __( 'Body POST Fields', 'wpgetapi' ),
350 'id' => 'body_parameters',
351 'type' => 'parameter',
352 'classes' => 'field-body-parameters',
353 'repeatable' => true,
354 'desc' => sprintf(
355 __( 'Parameters as name/value pairs, sent in the body as POST fields. %1s', 'wpgetapi' ),
356 '<a target="_blank" href="https://wpgetapi.com/docs/sending-headers-in-request/?utm_campaign=Body&utm_medium=Admin&utm_source=User">Help <span class="dashicons dashicons-external"></span></a>'
357 )
358 ),
359 array(
360 'name' => __( 'Encode Body', 'wpgetapi' ),
361 'id' => 'body_json_encode',
362 'type' => 'select',
363 'classes' => 'field-body-json-encode',
364 'options' => array(
365 'false' => __( 'No encoding', 'wpgetapi' ),
366 'true' => __( 'JSON encode', 'wpgetapi' ),
367 'url' => __( 'URL encode', 'wpgetapi' ),
368 ),
369 'desc' => __( 'Encoding of the above Body parameters (if they are set). ', 'wpgetapi' ),
370 ),
371 ) );
372
373
374 $fields[] = array(
375 'id' => 'endpoints',
376 'type' => 'group',
377 'name' => '',
378 'description' => '',
379 'options' => array(
380 'group_title' => __( 'Endpoint {#}', 'wpgetapi' ),
381 'add_button' => __( 'Add Endpoint', 'wpgetapi' ),
382 'remove_button' => __( 'Remove Endpoint', 'wpgetapi' ),
383 'sortable' => true, // beta
384 ),
385 'before_group' => '<pre class="url">Base URL: <span>' . $base_url . '</span></pre>',
386 'fields' => $endpoint_fields,
387 );
388
389 return apply_filters( 'wpgetapi_fields', $fields );
390
391 }
392
393
394
395
396 public function add_options_pages() {
397
398 $option_tabs = self::option_fields();
399
400 foreach ($option_tabs as $index => $option_tab) {
401 if ( $index == 0) {
402
403 $this->options_pages[] = add_menu_page( $this->title, $this->menu_title, 'manage_options', $option_tab['id'], array( $this, 'admin_page_display' ), 'dashicons-editor-code'
404 ); //Link admin menu to first tab
405
406 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
407 } else {
408 $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' ) );
409 }
410 }
411
412 foreach ( $this->options_pages as $page ) {
413 // Include CMB CSS in the head to avoid FOUC
414 add_action( "admin_print_styles-{$page}", array( 'CMB2_Hookup', 'enqueue_cmb_css' ) );
415 }
416
417 }
418
419 /**
420 * Admin page markup. Mmply handled by CMB2
421 * @since 0.1.0
422 */
423 public function admin_page_display() {
424
425 $option_tabs = self::option_fields(); //get all option tabs
426 $tab_forms = array();
427 ?>
428
429 <div class="wrap wpgetapi">
430
431 <div class="main_content_cell">
432
433 <h1 class="wp-heading-inline"><?php esc_html_e( $this->title, 'wpgetapi' ) ?></h1>
434 <!-- Options Page Nav Tabs -->
435 <h2 class="nav-tab-wrapper">
436 <?php foreach ($option_tabs as $option_tab) :
437 $tab_slug = $option_tab['id'];
438 $nav_class = 'nav-tab';
439 if ( $tab_slug == $_GET['page'] ) {
440 $nav_class .= ' nav-tab-active'; //add active class to current tab
441 $tab_forms[] = $option_tab; //add current tab to forms to be rendered
442 }
443 ?>
444 <a class="<?php esc_attr_e( $nav_class ); ?>" href="<?php esc_url( menu_page_url( $tab_slug ) ); ?>"><?php esc_attr_e( $option_tab['menu_title'], 'wpgetapi' ); ?></a>
445 <?php endforeach; ?>
446 </h2>
447 <!-- End of Nav Tabs -->
448
449 <?php foreach ($tab_forms as $tab_form) : //render all tab forms (normaly just 1 form)
450
451 ?>
452
453 <div id="<?php esc_attr_e($tab_form['id']); ?>" class="cmb-form group">
454 <div class="metabox-holder">
455 <div class="pmpbox pad">
456 <h3 class="title"><?php esc_html_e($tab_form['title'], 'wpgetapi'); ?></h3>
457 <div class="desc"><?php echo wp_kses_post( $tab_form['desc'] ); ?></div>
458 <?php cmb2_metabox_form( $tab_form, $tab_form['id'], $tab_form ); ?>
459 </div>
460 </div>
461 </div>
462
463 <?php endforeach; ?>
464
465 </div>
466
467 <div class="sidebar_cell">
468
469 <?php
470 $sidebar = self::sidebar_display();
471 echo apply_filters( 'wpgetapi_admin_sidebar_display', $sidebar );
472 ?>
473
474 </div>
475
476 </div>
477
478 <?php
479
480 }
481
482
483 public function test_the_endpoint() {
484
485 $api_id = sanitize_text_field( $_POST['api_id'] );
486 $endpoint_id = sanitize_text_field( $_POST['endpoint_id'] );
487 $data = wpgetapi_endpoint( $api_id, $endpoint_id, array( 'test_endpoint' => true ) );
488
489 $output = array(
490 'data' => $data,
491 'endpoint_id' => $endpoint_id
492 );
493 echo json_encode( $output );
494
495 wp_die(); // this is required to terminate immediately and return a proper response
496
497 }
498
499
500 public function load_testing_javascript() { ?>
501
502 <script type="text/javascript" >
503
504 jQuery(document).ready(function($) {
505
506 $('body').on( 'click', '.wpgetapi-test-area .test-button', function(){//delegated
507
508 var $this = $(this);
509 var area = $this.parents( '.wpgetapi-test-area' );
510 var api_id = $( area ).data( 'api' );
511 var endpoint_id = $( area ).data( 'endpoint' );
512
513 // disable button
514 $this.attr( 'disabled', true );
515
516 var data = {
517 'action': 'wpgetapi_test_endpoint',
518 'api_id': api_id,
519 'endpoint_id': endpoint_id
520 };
521
522 // send and get response
523 jQuery.post(ajaxurl, data, function( response ) {
524
525 var output = JSON.parse( response );
526
527 jQuery( '.wpgetapi-test-area[data-endpoint="' + output.endpoint_id + '"] .handle' ).show();
528
529 jQuery( '.wpgetapi-test-area[data-endpoint="' + output.endpoint_id + '"] .wpgetapi-result' ).html(
530 '<div class="side-notice"><p>These results contain the raw output of your API call as well as extra debugging information for testing purposes. <br>' +
531 'Using the shortcode or the template tag on the front end will display the Data Output section as shown below. The data can be formatted any way you like, depending on your requirements.</p></div>' +
532 output.data
533 );
534
535 // enable button
536 $( '.wpgetapi-test-area[data-endpoint="' + output.endpoint_id + '"] .test-button' ).attr( 'disabled', false );
537
538 });
539
540 });
541
542 $( '.wpgetapi-test-area .handle' ).on('click', function() {
543 $('.wpgetapi-result').toggle();
544 });
545
546 });
547
548 </script> <?php
549
550 }
551
552
553 /**
554 * Sidebar markup.
555 * @since 1.4.1
556 */
557 public function sidebar_display() {
558
559 ob_start();
560
561 // do our setup page
562 if( isset( $_GET['page'] ) && $_GET['page'] === 'wpgetapi_setup' )
563 return $this->sidebar_setup_page();
564
565 ?>
566
567 <div class="box">
568 <h3><?php esc_html_e( 'Endpoint Instructions', 'wpgetapi' ); ?></h3>
569 <ol>
570 <li><?php esc_html_e( 'Fill in the Unique ID with lowercase letters and underscores only. something_like_this', 'wpgetapi' ); ?></li>
571 <li><?php esc_html_e( 'Add the Endpoint which can be found in the docs of your API.', 'wpgetapi' ); ?></li>
572 <li><?php esc_html_e( 'Set the Method. Usually GET for getting data from the API and POST when sending data to the API.', 'wpgetapi' ); ?></li>
573 <li><?php esc_html_e( 'Set the desired Results Format.', 'wpgetapi' ); ?></li>
574 <li><?php esc_html_e( 'The rest of the fields are optional and will depend on the API you using.', 'wpgetapi' ); ?></li>
575 <li><?php esc_html_e( 'Hit the Save button.', 'wpgetapi' ); ?></li>
576 <li><?php esc_html_e( 'After saving, copy the Template Tag or the Shortcode and paste into appropriate place (page, post, theme file).', 'wpgetapi' ); ?></li>
577 </ol>
578 </div>
579
580 <hr>
581
582 <?php
583 if( ! $this->pro_plugin ) {
584 $this->pro_banner();
585 echo '<hr>';
586 }
587 ?>
588
589 <div class="box">
590 <strong><?php esc_html_e( 'Getting Help', 'wpgetapi' ); ?></strong>
591 <ul>
592 <li><?php
593 printf(
594 esc_html__( 'Visit our website to %1$s.', 'cmb2' ),
595 '<a target="_blank" href="https://wpgetapi.com/docs/?utm_campaign=Docs&utm_medium=Admin&utm_source=User">view the docs</a>'
596 );
597 ?></li>
598 </ul>
599 </div>
600
601 <?php
602 $content = ob_get_contents();
603 ob_end_clean();
604 return $content;
605
606 }
607
608
609 /**
610 * Sidebar for our setup page.
611 * @since 1.4.4
612 */
613 public function sidebar_setup_page() {
614
615 ob_start();
616 ?>
617
618 <div class="box">
619 <h3><?php esc_html_e( 'Setup Instructions', 'wpgetapi' ); ?></h3>
620 <ol>
621 <li><?php esc_html_e( 'Fill in the API Name. You can name this whatever you like.', 'wpgetapi' ); ?></li>
622 <li><?php esc_html_e( 'Fill in the Unique ID with lowercase letters and underscores only. something_like_this', 'wpgetapi' ); ?></li>
623 <li><?php esc_html_e( 'Add the Base URL of your API. This can be found in the docs of your API.', 'wpgetapi' ); ?></li>
624 <li><?php esc_html_e( 'Hit the Save button.', 'wpgetapi' ); ?></li>
625 <li><?php esc_html_e( 'After saving, visit the new tab that will be created.', 'wpgetapi' ); ?></li>
626 </ol>
627 </div>
628
629 <hr>
630
631 <?php
632 if( ! $this->pro_plugin ) {
633 $this->pro_banner();
634 echo '<hr>';
635 }
636 ?>
637
638 <div class="box">
639 <strong><?php esc_html_e( 'Getting Help', 'wpgetapi' ); ?></strong>
640 <ul>
641 <li><?php
642 printf(
643 esc_html__( 'Visit our website to %1$s.', 'cmb2' ),
644 '<a target="_blank" href="https://wpgetapi.com/docs/?utm_campaign=Docs&utm_medium=Admin&utm_source=User">view the docs</a>'
645 );
646 ?></li>
647 </ul>
648 </div>
649
650 <?php
651 $content = ob_get_contents();
652 ob_end_clean();
653 return $content;
654
655 }
656
657
658
659 /**
660 * Banner for the pro plugin.
661 * @since 1.4.4
662 */
663 public function pro_banner() {
664
665 ?>
666
667 <div class="box pro">
668 <h4><?php esc_html_e( 'Upgrade to the Pro Plugin', 'wpgetapi' ); ?></h4>
669 <ul>
670 <li><?php esc_html_e( 'Cache API calls', 'wpgetapi' ); ?></li>
671 <li><?php esc_html_e( 'Dynamic variables', 'wpgetapi' ); ?></li>
672 <li><?php esc_html_e( 'Get nested data', 'wpgetapi' ); ?></li>
673 <li><?php esc_html_e( 'Base64 encoding', 'wpgetapi' ); ?></li>
674 <li><?php esc_html_e( 'Format as HTML', 'wpgetapi' ); ?></li>
675 <li><?php esc_html_e( 'Format as a number', 'wpgetapi' ); ?></li>
676 <li><?php esc_html_e( 'XML format', 'wpgetapi' ); ?></li>
677 </ul>
678 <a class="button" href="https://wpgetapi.com/downloads/pro-plugin/"><?php esc_html_e( 'Get the Pro Plugin', 'wpgetapi' ); ?></a>
679 </div>
680
681 <?php
682
683 }
684
685 /**
686 * Tabs don't immmediately appear on save, so this hack!
687 * @since 0.1.0
688 */
689 public function redirect( $object_id, $updated ) {
690
691 if( $object_id == 'wpgetapi_setup' ) {
692
693 add_settings_error( 'wpgetapi-notices', '', 'Settings saved, reloading page...', 'updated' );
694 settings_errors( 'wpgetapi-notices' );
695 ?>
696
697 <script>
698 setTimeout(function() {
699 location.reload();
700 }, 1000);
701 </script>
702 <?php
703
704 } else if ( strpos( $object_id, 'wpgetapi_' ) !== false ) {
705
706 $text = apply_filters( 'wpgetapi_admin_notice_text', __( 'Settings saved.', 'wpgetapi' ), $object_id );
707 add_settings_error( 'wpgetapi-notices', '', $text, 'updated' );
708 settings_errors( 'wpgetapi-notices' );
709
710 }
711
712 }
713
714
715 /**
716 * Public getter method for retrieving protected/private variables
717 * @since 0.1.0
718 * @param string $field Field to retrieve
719 * @return mixed Field value or exception is thrown
720 */
721 public function __get( $field ) {
722 // Allowed fields to retrieve
723 if ( in_array( $field, array( 'key', 'fields', 'title', 'options_pages' ), true ) ) {
724 return $this->{$field};
725 }
726 if ( 'option_metabox' === $field ) {
727 return $this->option_fields();
728 }
729 throw new Exception( 'Invalid property: ' . $field );
730 }
731
732 }
733
734 /**
735 * Helper function to get/return the WpGetApi_Admin_Options object
736 * @since 0.1.0
737 * @return WpGetApi_Admin_Options object
738 */
739 function wpgetapi_admin_options() {
740 return WpGetApi_Admin_Options::get_instance();
741 }
742
743 // Get it started
744 wpgetapi_admin_options();
745
746 endif;