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

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