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

1,043 lines 33.1 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 add_action( 'wp_ajax_wpgetapi_export_endpoints', array( $this, 'export_endpoints' ) );
102 add_action( 'wp_ajax_wpgetapi_import_endpoints', array( $this, 'import_endpoints' ) );
103
104 }
105
106
107 /**
108 * Setup our custom fields
109 * @since 0.1.0
110 */
111 public function init_custom_fields() {
112 require_once WPGETAPIDIR . 'includes/class-fields.php';
113 WpGetApi_Parameter_Field::init_parameter();
114 }
115
116 /**
117 * Register our setting to WP
118 * @since 0.1.0
119 */
120 public function init() {
121 $this->pro_plugin = is_plugin_active( 'wpgetapi-extras/wpgetapi-extras.php' ) ? true : false;
122 $option_tabs = self::option_fields();
123 foreach ($option_tabs as $index => $option_tab) {
124 register_setting( $option_tab['id'], $option_tab['id'] );
125 }
126 }
127
128
129 /**
130 * Get our saved API's from setup
131 * @since 0.1.0
132 */
133 public function get_apis() {
134 $setup = get_option( 'wpgetapi_setup' );
135 if( empty( $setup ) || ! isset( $setup['apis'] ) || empty( $setup['apis'] ) )
136 return;
137 return $setup['apis'];
138 }
139
140
141 /**
142 * Add the options metabox to the array of metaboxes
143 * @since 0.1.0
144 */
145 public function option_fields() {
146
147 //Only need to initiate the array once per page-load
148 if ( ! empty( $this->option_metabox ) ) {
149 return $this->option_metabox;
150 }
151
152 // setup tab
153 $option_metabox[] = array(
154 'title' => __( 'Setup', 'wpgetapi' ),
155 'menu_title' => __( 'Setup', 'wpgetapi' ),
156 'id' => 'wpgetapi_setup',
157 'desc' => __( 'Add the details of the API(s) you are using. Endpoints will be setup in the next step after hitting save.', 'wpgetapi' ),
158 'show_on' => array( 'key' => 'options-page', 'value' => array( 'wpgetapi_setup' ), ),
159 'show_names' => true,
160 'fields' => $this->setup_fields(),
161 );
162
163 // get our saved API's and create as tabs
164 if( $this->get_apis() ) {
165
166 foreach ( $this->get_apis() as $index => $api ) {
167
168 $name = isset( $api['name'] ) && $api['name'] != '' ? sanitize_text_field( $api['name'] ) : 'API ' . absint( $index );
169 $api_id = isset( $api['id'] ) && $api['id'] != '' ? sanitize_text_field( $api['id'] ) : $name;
170 $metabox_id = strtolower( str_replace( '-', '_', sanitize_file_name( 'wpgetapi-' . $api_id ) ) );
171 $base_url = isset( $api['base_url'] ) && $api['base_url'] != '' ? esc_url_raw( $api['base_url'], array( 'http', 'https' ) ) : '';
172 $type = isset( $api['auth_type'] ) && $api['auth_type'] != '' ? sanitize_text_field( $api['auth_type'] ) : '';
173
174 // tab
175 $option_metabox[] = array(
176 'title' => $name,
177 'menu_title' => $name,
178 'id' => $metabox_id,
179 'desc' => '',
180 'show_on' => array( 'key' => 'options-page', 'value' => array( $metabox_id ), ),
181 'show_names' => true,
182 'fields' => $this->endpoint_fields( $type, $api_id, $base_url ),
183 );
184
185 }
186
187 }
188
189 $option_metabox = apply_filters( 'wpgetapi_admin_pages', $option_metabox );
190
191 return $option_metabox;
192
193 }
194
195 public function setup_fields() {
196
197 $fields[] = array(
198 'id' => 'apis',
199 'type' => 'group',
200 'name' => '',
201 'description' => '',
202 'options' => array(
203 'group_title' => __( 'API {#}', 'wpgetapi' ),
204 'add_button' => __( 'Add New API', 'wpgetapi' ),
205 'remove_button' => __( 'Remove API', 'wpgetapi' ),
206 'sortable' => true, // beta
207 'remove_confirm' => __( 'Are you sure you want to delete this API?', 'wpgetapi' ),
208 ),
209 'fields' => array(
210 array(
211 'name' => __( 'API Name', 'wpgetapi' ) . '<span class="dashicons dashicons-editor-help" data-tip="' . __( 'The name of this API. Call it whatever you like.', 'wpgetapi' ) . '"></span>',
212 'id' => 'name',
213 'type' => 'text',
214 'classes' => 'api-name',
215 'attributes' => array(
216 'required' => true,
217 'placeholder' => 'Google Maps',
218 ),
219 'desc' => '',
220 ),
221 array(
222 '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>',
223 'id' => 'id',
224 'type' => 'text',
225 'attributes' => array(
226 'required' => true,
227 'placeholder' => 'google_maps',
228 ),
229 'desc' => '',
230 'sanitization_cb' => 'wpgetapi_sanitize_unique_id'
231 ),
232 array(
233 'name' => __( 'Base URL', 'wpgetapi' ) . '<span class="dashicons dashicons-editor-help" data-tip="' . __( 'The base URL of the API you are connecting to.', 'wpgetapi' ) . '"></span>',
234 'id' => 'base_url',
235 'type' => 'text',
236 'attributes' => array(
237 'required' => true,
238 'placeholder' => 'https://maps.googleapis.com/maps/api',
239 ),
240 'desc' => '',
241 ),
242
243 )
244 );
245
246 return $fields;
247
248 }
249
250
251 /**
252 * Endpoint settings.
253 * @return sale info array
254 *
255 */
256 public function endpoint_fields( $type = '', $api_id = '', $base_url = '' ) {
257
258 $fields = array();
259
260 $cache_disabled = $this->pro_plugin ? '' : 'disabled';
261 $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>';
262 $cache_desc = $this->pro_plugin ? '' : 'Available in PRO plugin.';
263
264 $endpoint_fields = apply_filters( 'wpgetapi_fields_endpoints', array(
265 array(
266 '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>',
267 'id' => 'id',
268 'type' => 'text',
269 'classes' => 'field-id field-half',
270 'attributes' => array(
271 'required' => true,
272 'placeholder' => 'new_endpoint',
273 ),
274 'desc' => '',
275 'before_row' => "wpgetapi_output_top_of_endpoint",
276 'api_id' => $api_id,
277 'sanitization_cb' => 'wpgetapi_sanitize_unique_id'
278
279 ),
280 array(
281 '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>',
282 'id' => 'endpoint',
283 'type' => 'text',
284 'classes' => 'field-endpoint field-half',
285 'attributes' => array(
286 'required' => true,
287 'placeholder' => '/newendpoint',
288 ),
289 'desc' => '',
290 ),
291 array(
292 '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>',
293 'id' => 'method',
294 'type' => 'select',
295 'classes' => 'field-method field-half',
296 'attributes' => array(
297 'required' => true,
298 ),
299 'options' => array(
300 'GET' => 'GET',
301 'POST' => 'POST',
302 'PUT' => 'PUT',
303 'PATCH' => 'PATCH',
304 'DELETE' => 'DELETE',
305 ),
306 'desc' => '',
307 ),
308
309 array(
310 'name' => __( 'Results Format', 'wpgetapi' ) .
311 '<span class="dashicons dashicons-editor-help" data-tip="' .
312 sprintf(
313 __( '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' ),
314 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>' )
315 ) .
316 '"></span>',
317 'id' => 'results_format',
318 'type' => 'select',
319 'classes' => 'field-results-format field-half',
320 'attributes' => array(
321 'required' => true,
322 ),
323 'options_cb' => 'wpgetapi_results_format_options',
324 'desc' => ''
325 ),
326
327 array(
328 'name' => __( 'Timeout', 'wpgetapi' ) .
329 '<span class="dashicons dashicons-editor-help" data-tip="How long the connection should stay open in seconds. Default is 10. If you receive curl 28 errors, try increasing this value."></span>',
330 'id' => 'timeout',
331 'type' => 'text',
332 'default' => '10',
333 'classes' => 'field-timeout field-half',
334 'attributes' => array(
335 'placeholder' => '10',
336 ),
337 'desc' => ''
338 ),
339
340 array(
341 'name' => __( 'Cache Time', 'wpgetapi' ) .
342 '<span class="dashicons dashicons-editor-help" data-tip="' .
343 sprintf(
344 __( 'The time in seconds to cache this request for. %1s', 'wpgetapi' ),
345 htmlentities( '<br>' . $cache_url )
346 ) .
347 '"></span>',
348 'id' => 'cache_time',
349 'type' => 'text',
350 'classes' => 'field-cache-time field-half',
351 'attributes' => array(
352 'placeholder' => '3600',
353 $cache_disabled => $cache_disabled,
354 ),
355 'desc' => $cache_desc,
356 'after_row' => '</div>'
357 ),
358
359 array(
360 'name' => __( 'Query String', 'wpgetapi' ) .
361 '<span class="dashicons dashicons-editor-help" data-tip="' .
362 sprintf(
363 __( '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' ),
364 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>' )
365 ) .
366 '"></span>',
367 'id' => 'query_parameters',
368 'type' => 'parameter',
369 'classes' => 'field-query-parameters',
370 'repeatable' => true,
371 'desc' => '',
372 'before_row' => '<div class="fields-wrapper">'
373 ),
374 array(
375 'name' => __( 'Headers', 'wpgetapi' ) .
376 '<span class="dashicons dashicons-editor-help" data-tip="' .
377 sprintf(
378 __( 'Parameters as name/value pairs, sent in the Headers. %1s', 'wpgetapi' ),
379 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>' )
380 ) .
381 '"></span>',
382 'id' => 'header_parameters',
383 'type' => 'parameter',
384 'classes' => 'field-header-parameters',
385 'repeatable' => true,
386 'desc' => ''
387 ),
388 array(
389 'name' => __( 'Body', 'wpgetapi' ) .
390 '<span class="dashicons dashicons-editor-help" data-tip="' .
391 sprintf(
392 __( 'Parameters as name/value pairs, sent in the Body as POST fields. %1s', 'wpgetapi' ),
393 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>' )
394 ) .
395 '"></span>',
396 'id' => 'body_parameters',
397 'type' => 'parameter',
398 'classes' => 'field-body-parameters',
399 'repeatable' => true,
400 'desc' => ''
401 ),
402 array(
403 'name' => __( 'Encode Body', 'wpgetapi' ) . '<span class="dashicons dashicons-editor-help" data-tip="' . __( 'Encoding of the above Body parameters.', 'wpgetapi' ) . '"></span>',
404 'id' => 'body_json_encode',
405 'type' => 'select',
406 'classes' => 'field-body-json-encode',
407 'options' => array(
408 'false' => __( 'No encoding (raw)', 'wpgetapi' ),
409 'true' => __( 'JSON encode', 'wpgetapi' ),
410 'url' => __( 'URL encode (x-www-form-urlencoded)', 'wpgetapi' ),
411 'base64' => __( 'Base64 encode', 'wpgetapi' ),
412 'xml' => __( 'XML format (available in PRO)', 'wpgetapi' ),
413
414 ),
415 'desc' => '',
416 'after_row' => '</div>',
417 ),
418 array(
419 'name' => __( 'Actions', 'wpgetapi' ) . '<span class="dashicons dashicons-editor-help" data-tip="' .
420 sprintf(
421 __( 'Call this endpoint when your chosen action runs. This feature is available in the PRO plugin. %1s', 'wpgetapi' ),
422 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>' )
423 ) .
424 '"></span>',
425 'id' => 'actions',
426 'type' => 'select',
427 'classes' => 'field-actions',
428 'desc' => __( 'Available in PRO plugin.', 'wpgetapi' ),
429 'options' => array(
430 '' => __( '-- No Action --', 'wpgetapi-extras' ),
431 'new_post_published' => __( 'Post/Custom Post - New Post Published', 'wpgetapi-extras' ),
432 'transition_post_status' => __( 'Post/Custom Post - Status Changed', 'wpgetapi-extras' ),
433 'delete_post' => __( 'Post/Custom Post - Delete Post', 'wpgetapi-extras' ),
434
435 'user_register' => __( 'User - New User Registered', 'wpgetapi-extras' ),
436 'delete_user' => __( 'User - Delete User', 'wpgetapi-extras' ),
437 'user_login' => __( 'User - User Logs In', 'wpgetapi-extras' ),
438 'woocommerce_new_product' => __( 'WooCommerce - New Product Created', 'wpgetapi-extras' ),
439 'woocommerce_new_order' => __( 'WooCommerce - New Order Created', 'wpgetapi-extras' ),
440 'woocommerce_order_status_changed' => __( 'WooCommerce - Order Status Changed', 'wpgetapi-extras' ),
441 'contact_form_7' => __( 'Contact Form 7', 'wpgetapi-extras' ),
442 'gravity_forms' => __( 'Gravity Forms', 'wpgetapi-extras' ),
443 'wpforms' => __( 'WPForms', 'wpgetapi-extras' ),
444 'jetformbuilder' => __( 'JetFormBuilder', 'wpgetapi-extras' ),
445 'formidable_forms' => __( 'Formidable Forms', 'wpgetapi-extras' ),
446 'pmp' => __( 'Paid Memberships Pro - After Checkout', 'wpgetapi-extras' ),
447 'edd' => __( 'Easy Digital Downloads - Complete Purchase', 'wpgetapi-extras' ),
448 ),
449 'default' => '',
450 )
451
452 ) );
453
454 $fields[] = array(
455 'id' => 'endpoints',
456 'type' => 'group',
457 'name' => '',
458 'description' => '',
459 'options' => array(
460 'group_title' => __( 'Endpoint {#}', 'wpgetapi' ),
461 'add_button' => __( 'Add Endpoint', 'wpgetapi' ),
462 'remove_button' => __( 'Remove Endpoint', 'wpgetapi' ),
463 'sortable' => true, // beta
464 //'closed' => true,
465 'remove_confirm' => __( 'Are you sure you want to delete this endpoint?', 'wpgetapi' ),
466 ),
467 'before_group' => '<pre class="url"><div>Base URL:</div> <span>' . $base_url . '</span></pre>
468 <div class="import-export"><a href="#TB_inline?&width=600&height=550&inlineId=my-content-id" class="button thickbox">Import/Export</a></div>
469 <div class="collapse open"><a class="button" title="Collapse all endpoints"><span class="dashicons dashicons-arrow-down"></span></a></div>',
470 'fields' => $endpoint_fields,
471 );
472
473 return apply_filters( 'wpgetapi_fields', $fields );
474
475 }
476
477
478 public function add_options_pages() {
479 add_thickbox();
480
481 $option_tabs = self::option_fields();
482
483 foreach ($option_tabs as $index => $option_tab) {
484 if ( $index == 0) {
485
486 $this->options_pages[] = add_menu_page( $this->title, $this->menu_title, 'manage_options', $option_tab['id'], array( $this, 'admin_page_display' ), 'none'
487 ); //Link admin menu to first tab
488
489 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
490 } else {
491 $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' ) );
492 }
493 }
494
495 foreach ( $this->options_pages as $page ) {
496 // Include CMB CSS in the head to avoid FOUC
497 add_action( "admin_print_styles-{$page}", array( 'CMB2_Hookup', 'enqueue_cmb_css' ) );
498 }
499
500 }
501
502 /**
503 * Admin page markup. Mmply handled by CMB2
504 * @since 0.1.0
505 */
506 public function admin_page_display() {
507
508 $option_tabs = self::option_fields(); //get all option tabs
509 $tab_forms = array();
510
511 $l = '';
512 if( $this->pro_plugin ) {
513 $l = get_option( 'wpgetapi_pro_license_status' );
514 }
515
516 ?>
517
518 <div class="wrap wpgetapi <?php esc_attr_e( $l ); ?>">
519
520 <div class="main_content_cell">
521
522 <h1 class="wp-heading-inline">
523 <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>
524 </h1>
525
526 <div class="outer-box">
527 <!-- Options Page Nav Tabs -->
528 <h2 class="nav-tab-wrapper">
529 <?php foreach ($option_tabs as $option_tab) :
530
531 $tab_slug = $option_tab['id'];
532 $nav_class = 'nav-tab';
533 if ( $tab_slug == $_GET['page'] ) {
534 $nav_class .= ' nav-tab-active'; //add active class to current tab
535 $tab_forms[] = $option_tab; //add current tab to forms to be rendered
536 }
537
538 ?>
539
540 <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>
541
542 <?php endforeach; ?>
543 </h2>
544 <!-- End of Nav Tabs -->
545
546 <?php
547 //render all tab forms (normaly just 1 form)
548 foreach ($tab_forms as $tab_form) : ?>
549
550 <div id="<?php esc_attr_e($tab_form['id']); ?>" class="cmb-form group">
551 <div class="metabox-holder">
552
553 <div id="my-content-id" style="display:none;">
554 <div>
555 <?php echo $this->get_import_export(); ?>
556 </div>
557 </div>
558
559 <div class="pmpbox pad">
560 <h3 class="title"><?php esc_html_e($tab_form['title'], 'wpgetapi'); ?></h3>
561 <div class="desc"><?php echo wp_kses_post( $tab_form['desc'] ); ?></div>
562 <?php cmb2_metabox_form( $tab_form, $tab_form['id'], $tab_form ); ?>
563 </div>
564 </div>
565 </div>
566
567 <?php endforeach; ?>
568
569 </div>
570
571 </div>
572
573 <div class="sidebar_cell">
574
575 <?php
576 $sidebar = self::sidebar_display();
577 echo apply_filters( 'wpgetapi_admin_sidebar_display', $sidebar );
578 ?>
579
580 </div>
581
582 </div>
583
584 <?php
585
586 }
587
588
589 public function test_the_endpoint() {
590
591 $api_id = sanitize_text_field( $_POST['api_id'] );
592 $endpoint_id = sanitize_text_field( $_POST['endpoint_id'] );
593 $data = wpgetapi_endpoint( $api_id, $endpoint_id, array( 'test_endpoint' => true ) );
594
595 $output = array(
596 'data' => $data,
597 'endpoint_id' => $endpoint_id
598 );
599 echo json_encode( $output );
600
601 wp_die(); // this is required to terminate immediately and return a proper response
602
603 }
604
605 public function get_import_export() {
606 ob_start(); ?>
607
608 <div class="wpgetapi-export">
609 <h3><span class="dashicons dashicons-download"></span> Export Endpoints</h3>
610 <p>Export the endpoints for this API.<br>After clicking the button below, copy the entire contents of the export.</p>
611 <button class="button button-primary">Export Endpoints</button>
612 <div class="message"></div>
613 </div>
614
615 <div class="wpgetapi-import">
616 <h3><span class="dashicons dashicons-upload"></span> Import Endpoints</h3>
617 <p>Paste your exported JSON data into the field below to import endpoints into this API.</p>
618 <textarea></textarea>
619 <button class="button button-primary">Import Endpoints</button>
620 <div class="message"></div>
621 </div>
622
623 <?php
624 $content = ob_get_contents();
625 ob_end_clean();
626 return $content;
627 }
628
629 /**
630 * AJAX function to get our endpoints for export.
631 *
632 */
633 public function export_endpoints() {
634 $api_id = sanitize_text_field( $_POST['api_id'] );
635 $data = get_option( $api_id );
636
637 if( ! isset( $data['endpoints'] ) )
638 wp_die();
639
640 $encryption = new WpGetApi_Encryption();
641
642 foreach ( $data['endpoints'] as $i => $endpoint ) {
643 if( isset( $endpoint['query_parameters'] ) ) {
644 foreach ( $endpoint['query_parameters'] as $i2 => $query_parameters ) {
645 if( isset( $query_parameters['value'] ) )
646 $data['endpoints'][ $i ]['query_parameters'][ $i2 ]['value'] = $encryption->decrypt( $query_parameters['value'] );
647 }
648 }
649 if( isset( $endpoint['header_parameters'] ) ) {
650 foreach ( $endpoint['header_parameters'] as $i2 => $header_parameters ) {
651 if( isset( $header_parameters['value'] ) )
652 $data['endpoints'][ $i ]['header_parameters'][ $i2 ]['value'] = $encryption->decrypt( $header_parameters['value'] );
653 }
654 }
655 if( isset( $endpoint['body_parameters'] ) ) {
656 foreach ( $endpoint['body_parameters'] as $i2 => $body_parameters ) {
657 if( isset( $body_parameters['value'] ) )
658 $data['endpoints'][ $i ]['body_parameters'][ $i2 ]['value'] = $encryption->decrypt( $body_parameters['value'] );
659 }
660 }
661 }
662
663 echo json_encode( $data );
664 wp_die(); // this is required to terminate immediately and return a proper response
665 }
666
667 /**
668 * AJAX function to import our endpoints.
669 *
670 */
671 public function import_endpoints() {
672
673 if( ! current_user_can( 'manage_options' ) ) {
674 echo 'Admin privelages are required to import endpoints.';
675 wp_die();
676 }
677
678 if ( isset( $_POST['nonce'] ) ) {
679 if ( ! wp_verify_nonce( $_POST['nonce'], 'import_endpoint_nonce' ) ) {
680 echo 'Nope.';
681 wp_die();
682 }
683 }
684
685 $api_id = sanitize_text_field( $_POST['api_id'] );
686 $textarea = sanitize_textarea_field( $_POST['textarea'] );
687 $existing_data = get_option( $api_id );
688
689 if( ! $textarea ) {
690 echo 'No data found.';
691 wp_die();
692 }
693
694 $new_data = json_decode( stripslashes( $textarea ), true );
695 if( ! isset( $new_data['endpoints'] ) ){
696 echo 'No endpoints found.';
697 wp_die();
698 }
699
700 $encryption = new WpGetApi_Encryption();
701
702 foreach ( $new_data['endpoints'] as $i => $endpoint ) {
703 if( isset( $endpoint['query_parameters'] ) ) {
704 foreach ( $endpoint['query_parameters'] as $i2 => $query_parameters ) {
705 if( isset( $query_parameters['value'] ) )
706 $new_data['endpoints'][ $i ]['query_parameters'][ $i2 ]['value'] = $encryption->encrypt( $query_parameters['value'] );
707 }
708 }
709 if( isset( $endpoint['header_parameters'] ) ) {
710 foreach ( $endpoint['header_parameters'] as $i2 => $header_parameters ) {
711 if( isset( $header_parameters['value'] ) )
712 $new_data['endpoints'][ $i ]['header_parameters'][ $i2 ]['value'] = $encryption->encrypt( $header_parameters['value'] );
713 }
714 }
715 if( isset( $endpoint['body_parameters'] ) ) {
716 foreach ( $endpoint['body_parameters'] as $i2 => $body_parameters ) {
717 if( isset( $body_parameters['value'] ) )
718 $new_data['endpoints'][ $i ]['body_parameters'][ $i2 ]['value'] = $encryption->encrypt( $body_parameters['value'] );
719 }
720 }
721 }
722
723 // if no existing endpoints, simply add it
724 if( ! $existing_data ) {
725 update_option( $api_id, $new_data, 'no' );
726 } else {
727
728 $merged['endpoints'] = array_merge( $existing_data['endpoints'], $new_data['endpoints'] );
729 update_option( $api_id, $merged, 'no' );
730 }
731
732 echo 'Successfully imported. Reloading...';
733 wp_die(); // this is required to terminate immediately and return a proper response
734 }
735
736
737 public function load_testing_javascript() { ?>
738
739 <script type="text/javascript" >
740
741 jQuery(document).ready(function($) {
742
743 /**
744 * Collapse the endpoints
745 *
746 */
747 $('body').on( 'click', '.wpgetapi .collapse .button', function(){
748
749 var $this = $(this);
750 var $collapse = $(this).parents('.collapse');
751 var status = 'open';
752
753 $collapse.toggleClass('open');
754
755 if( ! $collapse.hasClass( 'open' ) )
756 status = 'closed';
757
758 var $endpoints = $( '#endpoints_repeat .postbox.cmb-repeatable-grouping' );
759 $endpoints.each( function() {
760
761 if( status == 'closed' ) {
762 $( this ).addClass('closed');
763 } else {
764 $( this ).removeClass('closed');
765 }
766
767 });
768
769 });
770
771 /**
772 * Run the export
773 *
774 */
775 $('body').on( 'click', '.wpgetapi-export .button', function(){
776
777 var $this = $(this);
778 var api_id = $('.wpgetapi.wrap').find( '.cmb-form.group' ).attr( 'id' );
779
780 // disable button
781 $this.attr( 'disabled', true );
782
783 var data = {
784 'action': 'wpgetapi_export_endpoints',
785 'api_id': api_id,
786 };
787
788 // send and get response
789 jQuery.post(ajaxurl, data, function( response ) {
790 $( '.wpgetapi-export .message' ).show().html( response );
791 // enable button
792 $( '.wpgetapi-export .button' ).attr( 'disabled', false );
793
794 });
795
796 });
797
798 /**
799 * Run the import
800 *
801 */
802 $('body').on( 'click', '.wpgetapi-import .button', function(){
803
804 var $this = $(this);
805 var api_id = $('.wpgetapi.wrap').find( '.cmb-form.group' ).attr( 'id' );
806 var textarea = $('.wpgetapi-import textarea').val();
807
808 // disable button
809 $this.attr( 'disabled', true );
810
811 var data = {
812 'action': 'wpgetapi_import_endpoints',
813 'textarea': textarea,
814 'api_id': api_id,
815 'nonce': "<?php echo wp_create_nonce( 'import_endpoint_nonce' ); ?>",
816 };
817
818 // send and get response
819 jQuery.post(ajaxurl, data, function( response ) {
820
821 $( '.wpgetapi-import .message' ).html( response );
822
823 if( response == 'Successfully imported. Reloading...' ) {
824 setTimeout(function(){
825 location.reload();
826 }, 1000);
827 } else {
828 $( '.wpgetapi-import .button' ).attr( 'disabled', false );
829 }
830
831 });
832
833 });
834
835 /**
836 * Run the endpoint test
837 *
838 */
839 $('body').on( 'click', '.wpgetapi .test-endpoint-button', function(){//delegated
840
841 var $this = $(this);
842 var $parent = $this.closest( '.cmb-repeatable-grouping' );
843 var area = $parent.find( '.wpgetapi-test-area' );
844 var api_id = $( area ).data( 'api' );
845 var endpoint_id = $( area ).data( 'endpoint' );
846
847 // disable button
848 $this.attr( 'disabled', true );
849
850 var data = {
851 'action': 'wpgetapi_test_endpoint',
852 'api_id': api_id,
853 'endpoint_id': endpoint_id
854 };
855
856 // send and get response
857 jQuery.post(ajaxurl, data, function( response ) {
858
859 var output = JSON.parse( response );
860
861 $( '.wpgetapi-test-area[data-endpoint="' + output.endpoint_id + '"] .handle' ).show();
862 $( '.wpgetapi-test-area[data-endpoint="' + output.endpoint_id + '"] .wpgetapi-result' ).html( output.data);
863
864 // enable button
865 $( '.wpgetapi .test-endpoint-button' ).attr( 'disabled', false );
866
867 });
868
869 });
870
871 $( '.wpgetapi-test-area .handle' ).on('click', function() {
872 $('.wpgetapi-result').toggle();
873 });
874
875 });
876
877 </script> <?php
878
879 }
880
881
882 /**
883 * Sidebar markup.
884 * @since 1.4.1
885 */
886 public function sidebar_display() {
887
888 ob_start();
889
890 // do our setup page
891 if( isset( $_GET['page'] ) && $_GET['page'] === 'wpgetapi_setup' )
892 return $this->sidebar_setup_page();
893
894 ?>
895
896 <div class="box">
897 <h4><?php esc_html_e( 'Endpoint Instructions', 'wpgetapi' ); ?></h4>
898 <ol>
899 <li><?php _e( 'Give your endpoint a <b>Unique ID</b>', 'wpgetapi' ); ?></li>
900 <li><?php _e( 'Add the <b>Endpoint</b> which can be found in your API docs', 'wpgetapi' ); ?></li>
901 <li><?php _e( 'Set the <b>Method</b> - GET for getting data & POST for sending data to the API', 'wpgetapi' ); ?></li>
902 <li><?php _e( 'Set the desired <b>Results Format</b>', 'wpgetapi' ); ?></li>
903 <li><?php _e( 'Other fields are optional & will depend on your API', 'wpgetapi' ); ?></li>
904 <li><?php _e( 'Hit the Save button', 'wpgetapi' ); ?></li>
905 <li><?php _e( 'Copy the Template Tag or Shortcode & paste into page, post or theme file', 'wpgetapi' ); ?></li>
906 </ol>
907 </div>
908
909 <?php echo $this->help_box(); ?>
910
911 <?php
912 $content = ob_get_contents();
913 ob_end_clean();
914 return $content;
915
916 }
917
918
919 /**
920 * Sidebar for our setup page.
921 * @since 1.4.4
922 */
923 public function sidebar_setup_page() {
924
925 ob_start();
926 ?>
927
928 <div class="box">
929 <h4><?php esc_html_e( 'Setup Instructions', 'wpgetapi' ); ?></h4>
930 <ol>
931 <li><?php _e( 'Name your API in the <b>API Name</b> field', 'wpgetapi' ); ?></li>
932 <li><?php _e( 'Give your API a <b>Unique ID</b>', 'wpgetapi' ); ?></li>
933 <li><?php _e( 'Add the <b>Base URL</b> of your API. This can be found in the docs of your API', 'wpgetapi' ); ?></li>
934 <li><?php _e( 'Hit the Save button', 'wpgetapi' ); ?></li>
935 <li><?php _e( 'Visit the new tab that will be created', 'wpgetapi' ); ?></li>
936 </ol>
937 </div>
938
939 <?php echo $this->help_box(); ?>
940
941 <?php
942 $content = ob_get_contents();
943 ob_end_clean();
944 return $content;
945
946 }
947
948
949 /**
950 *
951 * @since 1.4.4
952 */
953 public function help_box() {
954
955 ob_start();
956 ?>
957
958 <div class="box help-box">
959 <h4><?php esc_html_e( 'Getting Help', 'wpgetapi' ); ?></h4>
960 <ul>
961 <li><?php
962 printf(
963 esc_html__( 'View the %1$s', 'cmb2' ),
964 '<a target="_blank" href="https://wpgetapi.com/docs/quick-start-guide/?utm_campaign=Docs&utm_medium=Admin&utm_source=User">Quick Start Guide</a>'
965 );
966 ?></li>
967 <li><?php
968 printf(
969 esc_html__( 'View the %1$s', 'cmb2' ),
970 '<a target="_blank" href="https://wpgetapi.com/docs/frequently-asked-questions/?utm_campaign=Docs&utm_medium=Admin&utm_source=User">FAQs</a>'
971 );
972 ?></li>
973
974 <?php if( ! $this->pro_plugin ) { ?>
975 <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>
976 <?php } ?>
977 </ul>
978 </div>
979
980 <?php
981 $content = ob_get_contents();
982 ob_end_clean();
983 return $content;
984
985 }
986
987
988 /**
989 * Tabs don't immmediately appear on save, so this hack!
990 * @since 0.1.0
991 */
992 public function redirect( $object_id, $updated ) {
993
994 if( strpos( $object_id, 'wpgetapi_' ) !== false && strpos( $object_id, 'wpgetapi_api_importer' ) == false ) {
995
996 add_settings_error( 'wpgetapi-notices', '', 'Saved, reloading page...', 'updated' );
997 settings_errors( 'wpgetapi-notices' );
998 ?>
999
1000 <script>
1001 setTimeout(function() {
1002 location.reload();
1003 }, 200);
1004 </script>
1005 <?php
1006
1007 }
1008
1009 }
1010
1011
1012 /**
1013 * Public getter method for retrieving protected/private variables
1014 * @since 0.1.0
1015 * @param string $field Field to retrieve
1016 * @return mixed Field value or exception is thrown
1017 */
1018 public function __get( $field ) {
1019 // Allowed fields to retrieve
1020 if ( in_array( $field, array( 'key', 'fields', 'title', 'options_pages' ), true ) ) {
1021 return $this->{$field};
1022 }
1023 if ( 'option_metabox' === $field ) {
1024 return $this->option_fields();
1025 }
1026 throw new Exception( 'Invalid property: ' . $field );
1027 }
1028
1029 }
1030
1031 /**
1032 * Helper function to get/return the WpGetApi_Admin_Options object
1033 * @since 0.1.0
1034 * @return WpGetApi_Admin_Options object
1035 */
1036 function wpgetapi_admin_options() {
1037 return WpGetApi_Admin_Options::get_instance();
1038 }
1039
1040 // Get it started
1041 wpgetapi_admin_options();
1042
1043 endif;