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

778 lines 24.6 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 hitting save.', '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' ) . '<span class="dashicons dashicons-editor-help" data-tip="' . __( 'The name of this API. Call it whatever you like.', 'wpgetapi' ) . '"></span>',
208 'id' => 'name',
209 'type' => 'text',
210 'attributes' => array(
211 'required' => true,
212 'placeholder' => 'Google Maps',
213 ),
214 'desc' => '',
215 ),
216 array(
217 'name' => __( 'Unique ID', 'wpgetapi' ) . '<span class="dashicons dashicons-editor-help" data-tip="' . __( 'Choose your own unique ID for this API. Use lowercase letters and underscores only!', 'wpgetapi' ) . '"></span>',
218 'id' => 'id',
219 'type' => 'text',
220 'attributes' => array(
221 'required' => true,
222 'placeholder' => 'google_maps',
223 ),
224 'desc' => '',
225 'sanitization_cb' => 'wpgetapi_sanitize_unique_id'
226 ),
227 array(
228 'name' => __( 'Base URL', 'wpgetapi' ) . '<span class="dashicons dashicons-editor-help" data-tip="' . __( 'The base URL of the API you are connecting to.', 'wpgetapi' ) . '"></span>',
229 'id' => 'base_url',
230 'type' => 'text',
231 'attributes' => array(
232 'required' => true,
233 'placeholder' => 'https://maps.googleapis.com/maps/api',
234 ),
235 'desc' => '',
236 ),
237
238 )
239 );
240
241 return $fields;
242
243 }
244
245
246 /**
247 * Endpoint settings.
248 * @return sale info array
249 *
250 */
251 public function endpoint_fields( $type = '', $api_id = '', $base_url = '' ) {
252
253 $fields = array();
254
255 $cache_disabled = $this->pro_plugin ? '' : 'disabled';
256 $cache_url = $this->pro_plugin ? '<a target="_blank" href="https://wpgetapi.com/docs/caching-api-calls/">Caching Help</a>' : '<a target="_blank" href="https://wpgetapi.com/downloads/pro-plugin/?utm_campaign=Pro&utm_medium=Admin&utm_source=Cache">View PRO Plugin</a>';
257
258 $endpoint_fields = apply_filters( 'wpgetapi_fields_endpoints', array(
259 array(
260 'name' => __( 'Unique ID', 'wpgetapi' ) . '<span class="dashicons dashicons-editor-help" data-tip="' . __( 'Choose your own unique ID for this endpoint. Use lowercase letters and underscores only!', 'wpgetapi' ) . '"></span>',
261 'id' => 'id',
262 'type' => 'text',
263 'classes' => 'field-id',
264 'attributes' => array(
265 'required' => true,
266 'placeholder' => 'new_endpoint',
267 ),
268 'desc' => '',
269 'before_row' => "wpgetapi_output_top_of_endpoint",
270 'api_id' => $api_id,
271 'sanitization_cb' => 'wpgetapi_sanitize_unique_id'
272
273 ),
274 array(
275 'name' => __( 'Endpoint', 'wpgetapi' ) . '<span class="dashicons dashicons-editor-help" data-tip="' . __( 'This Endpoint will be appended to the Base URL to create the full URL that will be called.', 'wpgetapi' ) . '"></span>',
276 'id' => 'endpoint',
277 'type' => 'text',
278 'classes' => 'field-endpoint',
279 'attributes' => array(
280 'required' => true,
281 'placeholder' => '/newendpoint',
282 ),
283 'desc' => '',
284 ),
285 array(
286 'name' => __( 'Method', 'wpgetapi' ) . '<span class="dashicons dashicons-editor-help" data-tip="' . __( 'The Request Method for this endpoint. If you are displaying data from the API, it will usually be GET. If you are sending to the API, it will usually be POST.', 'wpgetapi' ) . '"></span>',
287 'id' => 'method',
288 'type' => 'select',
289 'classes' => 'field-method',
290 'attributes' => array(
291 'required' => true,
292 ),
293 'options' => array(
294 'GET' => 'GET',
295 'POST' => 'POST',
296 'PUT' => 'PUT',
297 'PATCH' => 'PATCH',
298 'DELETE' => 'DELETE',
299 ),
300 'desc' => '',
301 ),
302
303 array(
304 'name' => __( 'Results Format', 'wpgetapi' ) .
305 '<span class="dashicons dashicons-editor-help" data-tip="' .
306 sprintf(
307 __( 'The format that the API data will be returned in. To format the data as HTML, click the link below to learn how. %1s', 'wpgetapi' ),
308 htmlentities( '<br><a target="_blank" href="https://wpgetapi.com/docs/formatting-json-data/?utm_campaign=Shortcode JSON&utm_medium=Admin&utm_source=User">Format as HTML</a>' )
309 ) .
310 '"></span>',
311 'id' => 'results_format',
312 'type' => 'select',
313 'classes' => 'field-results-format',
314 'attributes' => array(
315 'required' => true,
316 ),
317 'options_cb' => 'wpgetapi_results_format_options',
318 'desc' => ''
319 ),
320
321 array(
322 'name' => __( 'Cache Time', 'wpgetapi' ) .
323 '<span class="dashicons dashicons-editor-help" data-tip="' .
324 sprintf(
325 __( 'The time in seconds to cache this request for. %1s', 'wpgetapi' ),
326 htmlentities( '<br>' . $cache_url )
327 ) .
328 '"></span>',
329 'id' => 'cache_time',
330 'type' => 'text',
331 'classes' => 'field-cache-time',
332 'attributes' => array(
333 'placeholder' => '3600',
334 $cache_disabled => $cache_disabled,
335 ),
336 'desc' => 'Available in PRO plugin.'
337 ),
338
339 array(
340 'name' => __( 'Query String', 'wpgetapi' ) .
341 '<span class="dashicons dashicons-editor-help" data-tip="' .
342 sprintf(
343 __( 'Parameters as name/value pairs that will be appended to the URL. The query string is the part of the URL after the question mark, like so: https://yoururl.com<b>?key=value&key3=value3</b> %1s', 'wpgetapi' ),
344 htmlentities( '<br><a target="_blank" href="https://wpgetapi.com/docs/adding-query-string-parameters/?utm_campaign=Query String&utm_medium=Admin&utm_source=User">More Info</a>' )
345 ) .
346 '"></span>',
347 'id' => 'query_parameters',
348 'type' => 'parameter',
349 'classes' => 'field-query-parameters',
350 'repeatable' => true,
351 'desc' => ''
352 ),
353 array(
354 'name' => __( 'Headers', 'wpgetapi' ) .
355 '<span class="dashicons dashicons-editor-help" data-tip="' .
356 sprintf(
357 __( 'Parameters as name/value pairs, sent in the Headers. %1s', 'wpgetapi' ),
358 htmlentities( '<br><a target="_blank" href="https://wpgetapi.com/docs/sending-headers-in-request/?utm_campaign=Headers&utm_medium=Admin&utm_source=User">More Info</a>' )
359 ) .
360 '"></span>',
361 'id' => 'header_parameters',
362 'type' => 'parameter',
363 'classes' => 'field-header-parameters',
364 'repeatable' => true,
365 'desc' => ''
366 ),
367 array(
368 'name' => __( 'Body POST Fields', 'wpgetapi' ) .
369 '<span class="dashicons dashicons-editor-help" data-tip="' .
370 sprintf(
371 __( 'Parameters as name/value pairs, sent in the Body as POST fields. %1s', 'wpgetapi' ),
372 htmlentities( '<br><a target="_blank" href="https://wpgetapi.com/docs/sending-post-fields-in-request/?utm_campaign=Body&utm_medium=Admin&utm_source=User">More Info</a>' )
373 ) .
374 '"></span>',
375 'id' => 'body_parameters',
376 'type' => 'parameter',
377 'classes' => 'field-body-parameters',
378 'repeatable' => true,
379 'desc' => ''
380 ),
381 array(
382 'name' => __( 'Encode Body', 'wpgetapi' ) . '<span class="dashicons dashicons-editor-help" data-tip="' . __( 'Encoding of the above Body parameters.', 'wpgetapi' ) . '"></span>',
383 'id' => 'body_json_encode',
384 'type' => 'select',
385 'classes' => 'field-body-json-encode',
386 'options' => array(
387 'false' => __( 'No encoding (raw)', 'wpgetapi' ),
388 'true' => __( 'JSON encode', 'wpgetapi' ),
389 'url' => __( 'URL encode (x-www-form-urlencoded)', 'wpgetapi' ),
390 'base64' => __( 'Base64 encode', 'wpgetapi' ),
391 'xml' => __( 'XML format (available in PRO)', 'wpgetapi' ),
392
393 ),
394 'desc' => '',
395 ),
396 array(
397 'name' => __( 'Actions', 'wpgetapi' ) . '<span class="dashicons dashicons-editor-help" data-tip="' .
398 sprintf(
399 __( 'Call this endpoint when your chosen action runs. This feature is available in the PRO plugin. %1s', 'wpgetapi' ),
400 htmlentities( '<br><a target="_blank" href="https://wpgetapi.com/downloads/pro-plugin/?utm_campaign=Pro&utm_medium=Admin&utm_source=Actions">View PRO Plugin</a>' )
401 ) .
402 '"></span>',
403 'id' => 'actions',
404 'type' => 'select',
405 'classes' => 'field-actions',
406 'desc' => __( 'Available in PRO plugin.', 'wpgetapi' ),
407 'options' => array(
408 '' => __( '-- No Action --', 'wpgetapi-extras' ),
409 'new_post_published' => __( 'Post/Custom Post - New Post Published', 'wpgetapi-extras' ),
410 'transition_post_status' => __( 'Post/Custom Post - Status Changed', 'wpgetapi-extras' ),
411 'delete_post' => __( 'Post/Custom Post - Delete Post', 'wpgetapi-extras' ),
412
413 'user_register' => __( 'User - New User Registered', 'wpgetapi-extras' ),
414 'delete_user' => __( 'User - Delete User', 'wpgetapi-extras' ),
415 'user_login' => __( 'User - User Logs In', 'wpgetapi-extras' ),
416 ),
417 'default' => '',
418 )
419
420 ) );
421
422
423 $fields[] = array(
424 'id' => 'endpoints',
425 'type' => 'group',
426 'name' => '',
427 'description' => '',
428 'options' => array(
429 'group_title' => __( 'Endpoint {#}', 'wpgetapi' ),
430 'add_button' => __( 'Add Endpoint', 'wpgetapi' ),
431 'remove_button' => __( 'Remove Endpoint', 'wpgetapi' ),
432 'sortable' => true, // beta
433 ),
434 'before_group' => '<pre class="url">Base URL: <span>' . $base_url . '</span></pre>',
435 'fields' => $endpoint_fields,
436 );
437
438 return apply_filters( 'wpgetapi_fields', $fields );
439
440 }
441
442
443 public function add_options_pages() {
444
445 $option_tabs = self::option_fields();
446
447 foreach ($option_tabs as $index => $option_tab) {
448 if ( $index == 0) {
449
450 $this->options_pages[] = add_menu_page( $this->title, $this->menu_title, 'manage_options', $option_tab['id'], array( $this, 'admin_page_display' ), 'none'
451 ); //Link admin menu to first tab
452
453 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
454 } else {
455 $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' ) );
456 }
457 }
458
459 foreach ( $this->options_pages as $page ) {
460 // Include CMB CSS in the head to avoid FOUC
461 add_action( "admin_print_styles-{$page}", array( 'CMB2_Hookup', 'enqueue_cmb_css' ) );
462 }
463
464 }
465
466 /**
467 * Admin page markup. Mmply handled by CMB2
468 * @since 0.1.0
469 */
470 public function admin_page_display() {
471
472 $option_tabs = self::option_fields(); //get all option tabs
473 $tab_forms = array();
474 ?>
475
476 <div class="wrap wpgetapi">
477
478 <div class="main_content_cell">
479
480 <h1 class="wp-heading-inline">
481 <img width="24" height="22" src="<?php echo esc_url( WPGETAPIURL . 'assets/img/wpgetapi-icon.png' ); ?>" /> <?php esc_html_e( $this->title, 'wpgetapi' ) ?> <span class="vnum"><?php echo WPGETAPIVERSION; ?></span>
482 </h1>
483 <!-- Options Page Nav Tabs -->
484 <h2 class="nav-tab-wrapper">
485 <?php foreach ($option_tabs as $option_tab) :
486
487 $tab_slug = $option_tab['id'];
488 $nav_class = 'nav-tab';
489 if ( $tab_slug == $_GET['page'] ) {
490 $nav_class .= ' nav-tab-active'; //add active class to current tab
491 $tab_forms[] = $option_tab; //add current tab to forms to be rendered
492 }
493
494 ?>
495
496 <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>
497
498 <?php endforeach; ?>
499 </h2>
500 <!-- End of Nav Tabs -->
501
502 <?php
503 //render all tab forms (normaly just 1 form)
504 foreach ($tab_forms as $tab_form) : ?>
505
506 <div id="<?php esc_attr_e($tab_form['id']); ?>" class="cmb-form group">
507 <div class="metabox-holder">
508 <div class="pmpbox pad">
509 <h3 class="title"><?php esc_html_e($tab_form['title'], 'wpgetapi'); ?></h3>
510 <div class="desc"><?php echo wp_kses_post( $tab_form['desc'] ); ?></div>
511 <?php cmb2_metabox_form( $tab_form, $tab_form['id'], $tab_form ); ?>
512 </div>
513 </div>
514 </div>
515
516 <?php endforeach; ?>
517
518 </div>
519
520 <div class="sidebar_cell">
521
522 <?php
523 $sidebar = self::sidebar_display();
524 echo apply_filters( 'wpgetapi_admin_sidebar_display', $sidebar );
525 ?>
526
527 </div>
528
529 </div>
530
531 <?php
532
533 }
534
535
536 public function test_the_endpoint() {
537
538 $api_id = sanitize_text_field( $_POST['api_id'] );
539 $endpoint_id = sanitize_text_field( $_POST['endpoint_id'] );
540 $data = wpgetapi_endpoint( $api_id, $endpoint_id, array( 'test_endpoint' => true ) );
541
542 $output = array(
543 'data' => $data,
544 'endpoint_id' => $endpoint_id
545 );
546 echo json_encode( $output );
547
548 wp_die(); // this is required to terminate immediately and return a proper response
549
550 }
551
552
553 public function load_testing_javascript() { ?>
554
555 <script type="text/javascript" >
556
557 jQuery(document).ready(function($) {
558
559 $('body').on( 'click', '.wpgetapi-test-area .test-button', function(){//delegated
560
561 var $this = $(this);
562 var area = $this.parents( '.wpgetapi-test-area' );
563 var api_id = $( area ).data( 'api' );
564 var endpoint_id = $( area ).data( 'endpoint' );
565
566 // disable button
567 $this.attr( 'disabled', true );
568
569 var data = {
570 'action': 'wpgetapi_test_endpoint',
571 'api_id': api_id,
572 'endpoint_id': endpoint_id
573 };
574
575 // send and get response
576 jQuery.post(ajaxurl, data, function( response ) {
577
578 var output = JSON.parse( response );
579
580 jQuery( '.wpgetapi-test-area[data-endpoint="' + output.endpoint_id + '"] .handle' ).show();
581
582 jQuery( '.wpgetapi-test-area[data-endpoint="' + output.endpoint_id + '"] .wpgetapi-result' ).html( output.data);
583
584 // enable button
585 $( '.wpgetapi-test-area[data-endpoint="' + output.endpoint_id + '"] .test-button' ).attr( 'disabled', false );
586
587 });
588
589 });
590
591 $( '.wpgetapi-test-area .handle' ).on('click', function() {
592 $('.wpgetapi-result').toggle();
593 });
594
595 });
596
597 </script> <?php
598
599 }
600
601
602 /**
603 * Sidebar markup.
604 * @since 1.4.1
605 */
606 public function sidebar_display() {
607
608 ob_start();
609
610 // do our setup page
611 if( isset( $_GET['page'] ) && $_GET['page'] === 'wpgetapi_setup' )
612 return $this->sidebar_setup_page();
613
614 ?>
615
616 <div class="box">
617 <strong><?php esc_html_e( 'Endpoint Instructions', 'wpgetapi' ); ?></strong>
618 <ol>
619 <li><?php _e( 'Give your endpoint a <b>Unique ID</b>', 'wpgetapi' ); ?></li>
620 <li><?php _e( 'Add the <b>Endpoint</b> which can be found in your API docs', 'wpgetapi' ); ?></li>
621 <li><?php _e( 'Set the <b>Method</b> - GET for getting data & POST for sending data to the API', 'wpgetapi' ); ?></li>
622 <li><?php _e( 'Set the desired <b>Results Format</b>', 'wpgetapi' ); ?></li>
623 <li><?php _e( 'Other fields are optional & will depend on your API', 'wpgetapi' ); ?></li>
624 <li><?php _e( 'Hit the Save button', 'wpgetapi' ); ?></li>
625 <li><?php _e( 'Copy the Template Tag or Shortcode & paste into page, post or theme file', 'wpgetapi' ); ?></li>
626 </ol>
627 </div>
628
629 <hr>
630
631 <div class="box">
632 <strong><?php esc_html_e( 'Getting Help', 'wpgetapi' ); ?></strong>
633 <ul>
634 <li><?php
635 printf(
636 esc_html__( 'View the %1$s', 'cmb2' ),
637 '<a target="_blank" href="https://wpgetapi.com/docs/quick-start-guide/?utm_campaign=Docs&utm_medium=Admin&utm_source=User">Quick Start Guide</a>'
638 );
639 ?></li>
640 <li><?php
641 printf(
642 esc_html__( 'View the %1$s', 'cmb2' ),
643 '<a target="_blank" href="https://wpgetapi.com/docs/frequently-asked-questions/?utm_campaign=Docs&utm_medium=Admin&utm_source=User">FAQs</a>'
644 );
645 ?></li>
646 <li><?php
647 printf(
648 esc_html__( 'Do more with the %1$s', 'cmb2' ),
649 '<a target="_blank" href="https://wpgetapi.com/downloads/pro-plugin/?utm_campaign=Pro&utm_medium=Admin&utm_source=User">PRO Plugin</a>'
650 );
651 ?></li>
652 </ul>
653 </div>
654
655 <?php
656 $content = ob_get_contents();
657 ob_end_clean();
658 return $content;
659
660 }
661
662
663 /**
664 * Sidebar for our setup page.
665 * @since 1.4.4
666 */
667 public function sidebar_setup_page() {
668
669 ob_start();
670 ?>
671
672 <div class="box">
673 <strong><?php esc_html_e( 'Setup Instructions', 'wpgetapi' ); ?></strong>
674 <ol>
675 <li><?php _e( 'Name your API in the <b>API Name</b> field', 'wpgetapi' ); ?></li>
676 <li><?php _e( 'Give your API a <b>Unique ID</b>', 'wpgetapi' ); ?></li>
677 <li><?php _e( 'Add the <b>Base URL</b> of your API. This can be found in the docs of your API', 'wpgetapi' ); ?></li>
678 <li><?php _e( 'Hit the Save button', 'wpgetapi' ); ?></li>
679 <li><?php _e( 'Visit the new tab that will be created', 'wpgetapi' ); ?></li>
680 </ol>
681 </div>
682
683 <hr>
684
685 <div class="box">
686 <strong><?php esc_html_e( 'Getting Help', 'wpgetapi' ); ?></strong>
687 <ul>
688 <li><?php
689 printf(
690 esc_html__( 'View the %1$s', 'cmb2' ),
691 '<a target="_blank" href="https://wpgetapi.com/docs/quick-start-guide/?utm_campaign=Docs&utm_medium=Admin&utm_source=User">Quick Start Guide</a>'
692 );
693 ?></li>
694 <li><?php
695 printf(
696 esc_html__( 'View the %1$s', 'cmb2' ),
697 '<a target="_blank" href="https://wpgetapi.com/docs/frequently-asked-questions/?utm_campaign=Docs&utm_medium=Admin&utm_source=User">FAQs</a>'
698 );
699 ?></li>
700 <li><?php
701 printf(
702 esc_html__( 'Do more with the %1$s', 'cmb2' ),
703 '<a target="_blank" href="https://wpgetapi.com/downloads/pro-plugin/?utm_campaign=Pro&utm_medium=Admin&utm_source=User">PRO Plugin</a>'
704 );
705 ?></li>
706 </ul>
707 </div>
708
709 <?php
710 $content = ob_get_contents();
711 ob_end_clean();
712 return $content;
713
714 }
715
716
717 /**
718 * Tabs don't immmediately appear on save, so this hack!
719 * @since 0.1.0
720 */
721 public function redirect( $object_id, $updated ) {
722
723 if( $object_id == 'wpgetapi_setup' ) {
724
725 add_settings_error( 'wpgetapi-notices', '', 'Saved, reloading page...', 'updated' );
726 settings_errors( 'wpgetapi-notices' );
727 ?>
728
729 <script>
730 setTimeout(function() {
731 location.reload();
732 }, 500);
733 </script>
734 <?php
735
736 } else if ( strpos( $object_id, 'wpgetapi_' ) !== false ) {
737
738 $text = apply_filters( 'wpgetapi_admin_notice_text', __( 'Saved.', 'wpgetapi' ), $object_id );
739 add_settings_error( 'wpgetapi-notices', '', $text, 'updated' );
740 settings_errors( 'wpgetapi-notices' );
741
742 }
743
744 }
745
746
747 /**
748 * Public getter method for retrieving protected/private variables
749 * @since 0.1.0
750 * @param string $field Field to retrieve
751 * @return mixed Field value or exception is thrown
752 */
753 public function __get( $field ) {
754 // Allowed fields to retrieve
755 if ( in_array( $field, array( 'key', 'fields', 'title', 'options_pages' ), true ) ) {
756 return $this->{$field};
757 }
758 if ( 'option_metabox' === $field ) {
759 return $this->option_fields();
760 }
761 throw new Exception( 'Invalid property: ' . $field );
762 }
763
764 }
765
766 /**
767 * Helper function to get/return the WpGetApi_Admin_Options object
768 * @since 0.1.0
769 * @return WpGetApi_Admin_Options object
770 */
771 function wpgetapi_admin_options() {
772 return WpGetApi_Admin_Options::get_instance();
773 }
774
775 // Get it started
776 wpgetapi_admin_options();
777
778 endif;