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

616 lines 18.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings Required
4 *
5 */
6
7 // Exit if accessed directly.
8 if ( ! defined( 'ABSPATH' ) ) exit;
9
10 if ( ! class_exists( 'WpGetApi_Admin_Options' ) ) :
11
12 /**
13 * CMB2 Theme Options
14 * @version 0.1.0
15 */
16 class WpGetApi_Admin_Options {
17
18 /**
19 * Array of metaboxes/fields
20 * @var array
21 */
22 public $option_metabox = array();
23
24 /**
25 * Array of metaboxes/fields
26 * @var array
27 */
28 public $metabox_id = '';
29
30 /**
31 * Options Page title
32 * @var string
33 */
34 protected $title = '';
35
36 /**
37 * Options Page title
38 * @var string
39 */
40 protected $menu_title = '';
41
42 /**
43 * Options Tab Pages
44 * @var array
45 */
46 public $options_pages = array();
47
48 /**
49 * Holds an instance of the object
50 *
51 * @var Myprefix_Admin
52 **/
53 private static $instance = null;
54
55 /**
56 * Constructor
57 * @since 0.1.0
58 */
59 private function __construct() {
60 // Set our title
61 $this->menu_title = __( 'WPGetAPI', 'wpgetapi' );
62 $this->title = __( 'WPGetAPI', 'wpgetapi' );
63 }
64
65 /**
66 * Returns the running object
67 *
68 * @return Myprefix_Admin
69 **/
70 public static function get_instance() {
71 if( is_null( self::$instance ) ) {
72 self::$instance = new self();
73 self::$instance->hooks();
74 }
75 return self::$instance;
76 }
77
78 /**
79 * Initiate our hooks
80 * @since 0.1.0
81 */
82 public function hooks() {
83 add_action( 'admin_init', array( $this, 'init' ) );
84 add_action( 'admin_menu', array( $this, 'add_options_pages' ) );
85
86 add_action( 'cmb2_admin_init', array( $this, 'init_custom_fields' ) );
87
88 add_action( "cmb2_save_options-page_fields", array( $this, 'redirect' ), 1, 2 );
89 }
90
91 /**
92 * Setup our custom fields
93 * @since 0.1.0
94 */
95 public function init_custom_fields() {
96 require_once WPGETAPIDIR . 'includes/class-fields.php';
97 WpGetApi_Parameter_Field::init_parameter();
98 }
99
100 /**
101 * Register our setting to WP
102 * @since 0.1.0
103 */
104 public function init() {
105 $option_tabs = self::option_fields();
106 foreach ($option_tabs as $index => $option_tab) {
107 register_setting( $option_tab['id'], $option_tab['id'] );
108 }
109 }
110
111
112 /**
113 * Get our saved API's from setup
114 * @since 0.1.0
115 */
116 public function get_apis() {
117 $setup = get_option( 'wpgetapi_setup' );
118 if( empty( $setup ) || ! isset( $setup['apis'] ) || empty( $setup['apis'] ) )
119 return;
120 return $setup['apis'];
121 }
122
123
124 /**
125 * Add the options metabox to the array of metaboxes
126 * @since 0.1.0
127 */
128 public function option_fields() {
129
130 //Only need to initiate the array once per page-load
131 if ( ! empty( $this->option_metabox ) ) {
132 return $this->option_metabox;
133 }
134
135 // setup tab
136 $option_metabox[] = array(
137 'title' => __( 'Setup', 'wpgetapi' ),
138 'menu_title' => __( 'Setup', 'wpgetapi' ),
139 'id' => 'wpgetapi_setup',
140 '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' ),
141 'show_on' => array( 'key' => 'options-page', 'value' => array( 'wpgetapi_setup' ), ),
142 'show_names' => true,
143 'fields' => $this->setup_fields(),
144 );
145
146 // get our saved API's and create as tabs
147 if( $this->get_apis() ) {
148
149 foreach ( $this->get_apis() as $index => $api ) {
150
151 $name = isset( $api['name'] ) && $api['name'] != '' ? sanitize_text_field( $api['name'] ) : 'API ' . absint( $index );
152 $api_id = isset( $api['id'] ) && $api['id'] != '' ? sanitize_text_field( $api['id'] ) : $name;
153 $metabox_id = strtolower( str_replace( '-', '_', sanitize_file_name( 'wpgetapi-' . $api_id ) ) );
154 $base_url = isset( $api['base_url'] ) && $api['base_url'] != '' ? esc_url_raw( $api['base_url'], array( 'http', 'https' ) ) : '';
155 $type = isset( $api['auth_type'] ) && $api['auth_type'] != '' ? sanitize_text_field( $api['auth_type'] ) : '';
156
157 // tab
158 $option_metabox[] = array(
159 'title' => $name,
160 'menu_title' => $name,
161 'id' => $metabox_id,
162 'desc' => '',
163 'show_on' => array( 'key' => 'options-page', 'value' => array( $metabox_id ), ),
164 'show_names' => true,
165 'fields' => $this->api_fields( $type, $api_id, $base_url ),
166 );
167
168 }
169
170 }
171
172 $option_metabox = apply_filters( 'wpgetapi_admin_pages', $option_metabox );
173
174 return $option_metabox;
175
176 }
177
178 public function setup_fields() {
179
180 $fields[] = array(
181 'id' => 'apis',
182 'type' => 'group',
183 'name' => '',
184 'description' => '',
185 'options' => array(
186 'group_title' => __( 'API {#}', 'wpgetapi' ),
187 'add_button' => __( 'Add New API', 'wpgetapi' ),
188 'remove_button' => __( 'Remove API', 'wpgetapi' ),
189 'sortable' => true, // beta
190 ),
191 'fields' => array(
192 array(
193 'name' => __( 'API Name', 'wpgetapi' ),
194 'id' => 'name',
195 'type' => 'text',
196 'attributes' => array(
197 'required' => true,
198 'placeholder' => 'Google Maps',
199 ),
200 'desc' => __( 'The name of the API you are connecting to.', 'wpgetapi' ),
201 ),
202 array(
203 'name' => __( 'Unique ID', 'wpgetapi' ),
204 'id' => 'id',
205 'type' => 'text',
206 'attributes' => array(
207 'required' => true,
208 'placeholder' => 'google_maps',
209 ),
210 'desc' => __( 'A unique ID for your API.', 'wpgetapi' ),
211 ),
212 array(
213 'name' => __( 'Base URL', 'wpgetapi' ),
214 'id' => 'base_url',
215 'type' => 'text',
216 'attributes' => array(
217 'required' => true,
218 'placeholder' => 'http://maps.googleapis.com/maps/api',
219 ),
220 'desc' => __( 'The base URL of the API you are connecting to.', 'wpgetapi' ),
221 ),
222
223 )
224 );
225
226 return $fields;
227
228 }
229
230
231 /**
232 * Site wide info on the current sale.
233 * @return sale info array
234 *
235 */
236 public function api_fields( $type = '', $api_id = '', $base_url = '' ) {
237
238 $fields = array();
239
240 $endpoint_fields = apply_filters( 'wpgetapi_fields_endpoints', array(
241 array(
242 'name' => __( 'Unique ID', 'wpgetapi' ),
243 'id' => 'id',
244 'type' => 'text',
245 'classes' => 'field-id',
246 'attributes' => array(
247 'required' => true,
248 'placeholder' => 'new_endpoint',
249 ),
250 'desc' => __( 'A unique ID for this endpoint.', 'wpgetapi' ),
251 'before_row' => "
252 <pre class='functions'>
253 Template Tag: </span><span>wpgetapi_endpoint( '" . $api_id . "', '<span class='endpoint_id'></span>', array('debug' => false) );</span><br>
254 Shortcode: <span>[wpgetapi_endpoint api_id='" . $api_id . "' endpoint_id='<span class='endpoint_id'></span>' debug='false']</span>
255 </pre>
256 ",
257 ),
258 array(
259 'name' => __( 'Endpoint', 'wpgetapi' ),
260 'id' => 'endpoint',
261 'type' => 'text',
262 'classes' => 'field-endpoint',
263 'attributes' => array(
264 'required' => true,
265 'placeholder' => '/newendpoint',
266 ),
267 'desc' => __( 'The endpoint that will be appended to the base URL.', 'wpgetapi' ),
268 ),
269 array(
270 'name' => __( 'Method', 'wpgetapi' ),
271 'id' => 'method',
272 'type' => 'select',
273 'classes' => 'field-method',
274 'attributes' => array(
275 'required' => true,
276 ),
277 'options' => array(
278 'GET' => 'GET',
279 'POST' => 'POST',
280 ),
281 'desc' => __( 'The request method for this endpoint.', 'wpgetapi' ),
282 ),
283 array(
284 'name' => __( 'Results Format', 'wpgetapi' ),
285 'id' => 'results_format',
286 'type' => 'select',
287 'classes' => 'field-results-format',
288 'attributes' => array(
289 'required' => true,
290 ),
291 'options_cb' => 'wpgetapi_results_format_options',
292 'desc' => __( 'The format of the results.', 'wpgetapi' ),
293 ),
294 array(
295 'name' => __( 'Query String', 'wpgetapi' ),
296 'id' => 'query_parameters',
297 'type' => 'parameter',
298 'classes' => 'field-query-parameters',
299 'repeatable' => true,
300 'desc' => sprintf(
301 __( 'Parameters as name/value pairs that will be appended to the URL. %1s', 'wpgetapi' ),
302 '<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>'
303 )
304 ),
305 array(
306 'name' => __( 'Headers', 'wpgetapi' ),
307 'id' => 'header_parameters',
308 'type' => 'parameter',
309 'classes' => 'field-header-parameters',
310 'repeatable' => true,
311 'desc' => sprintf(
312 __( 'Parameters as name/value pairs, sent in the headers. %1s', 'wpgetapi' ),
313 '<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>'
314 )
315 ),
316 array(
317 'name' => __( 'Body POST Fields', 'wpgetapi' ),
318 'id' => 'body_parameters',
319 'type' => 'parameter',
320 'classes' => 'field-body-parameters',
321 'repeatable' => true,
322 'desc' => sprintf(
323 __( 'Parameters as name/value pairs, sent in the body as POST fields. %1s', 'wpgetapi' ),
324 '<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>'
325 )
326 ),
327 array(
328 'name' => __( 'Encode Body', 'wpgetapi' ),
329 'id' => 'body_json_encode',
330 'type' => 'select',
331 'classes' => 'field-body-json-encode',
332 'options' => array(
333 'false' => __( 'No encoding', 'wpgetapi' ),
334 'true' => __( 'JSON encode', 'wpgetapi' ),
335 'url' => __( 'URL encode', 'wpgetapi' ),
336 ),
337 'desc' => __( 'Should Body parameters be encoded (if set). ', 'wpgetapi' ),
338 ),
339 ) );
340
341
342 $fields[] = array(
343 'id' => 'endpoints',
344 'type' => 'group',
345 'name' => '',
346 'description' => '',
347 'options' => array(
348 'group_title' => __( 'Endpoint {#}', 'wpgetapi' ),
349 'add_button' => __( 'Add Endpoint', 'wpgetapi' ),
350 'remove_button' => __( 'Remove Endpoint', 'wpgetapi' ),
351 'sortable' => true, // beta
352 ),
353 'before_group' => '<pre class="url">Base URL: <span>' . $base_url . '</span></pre>',
354 'fields' => $endpoint_fields,
355 );
356
357 return apply_filters( 'wpgetapi_fields', $fields );
358
359 }
360
361
362 public function add_options_pages() {
363
364 $option_tabs = self::option_fields();
365
366 foreach ($option_tabs as $index => $option_tab) {
367 if ( $index == 0) {
368
369 $this->options_pages[] = add_menu_page( $this->title, $this->menu_title, 'manage_options', $option_tab['id'], array( $this, 'admin_page_display' ), 'dashicons-editor-code'
370 ); //Link admin menu to first tab
371
372 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
373 } else {
374 $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' ) );
375 }
376 }
377
378 foreach ( $this->options_pages as $page ) {
379 // Include CMB CSS in the head to avoid FOUC
380 add_action( "admin_print_styles-{$page}", array( 'CMB2_Hookup', 'enqueue_cmb_css' ) );
381 }
382
383 }
384
385 /**
386 * Admin page markup. Mmply handled by CMB2
387 * @since 0.1.0
388 */
389 public function admin_page_display() {
390
391 $option_tabs = self::option_fields(); //get all option tabs
392 $tab_forms = array();
393 ?>
394
395 <div class="wrap wpgetapi">
396
397 <div class="main_content_cell">
398
399 <h1 class="wp-heading-inline"><?php esc_html_e( $this->title, 'wpgetapi' ) ?></h1>
400 <!-- Options Page Nav Tabs -->
401 <h2 class="nav-tab-wrapper">
402 <?php foreach ($option_tabs as $option_tab) :
403 $tab_slug = $option_tab['id'];
404 $nav_class = 'nav-tab';
405 if ( $tab_slug == $_GET['page'] ) {
406 $nav_class .= ' nav-tab-active'; //add active class to current tab
407 $tab_forms[] = $option_tab; //add current tab to forms to be rendered
408 }
409 ?>
410 <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>
411 <?php endforeach; ?>
412 </h2>
413 <!-- End of Nav Tabs -->
414
415 <?php foreach ($tab_forms as $tab_form) : //render all tab forms (normaly just 1 form)
416
417 ?>
418
419 <div id="<?php esc_attr_e($tab_form['id']); ?>" class="cmb-form group">
420 <div class="metabox-holder">
421 <div class="pmpbox pad">
422 <h3 class="title"><?php esc_html_e($tab_form['title'], 'wpgetapi'); ?></h3>
423 <div class="desc"><?php echo wp_kses_post( $tab_form['desc'] ); ?></div>
424 <?php cmb2_metabox_form( $tab_form, $tab_form['id'], $tab_form ); ?>
425 </div>
426 </div>
427 </div>
428
429 <?php endforeach; ?>
430
431 </div>
432
433 <div class="sidebar_cell">
434
435 <?php
436 $sidebar = self::sidebar_display();
437 echo apply_filters( 'wpgetapi_admin_sidebar_display', $sidebar );
438 ?>
439
440 </div>
441
442 </div>
443
444 <?php
445
446 }
447
448
449 /**
450 * Sidebar markup.
451 * @since 1.4.1
452 */
453 public function sidebar_display() {
454
455 ob_start();
456
457 // do our setup page
458 if( isset( $_GET['page'] ) && $_GET['page'] === 'wpgetapi_setup' )
459 return $this->sidebar_setup_page();
460
461 ?>
462
463 <div class="box">
464 <h3><?php esc_html_e( 'Endpoint Instructions', 'wpgetapi' ); ?></h3>
465 <ol>
466 <li><?php esc_html_e( 'Fill in the Unique ID and Endpoint.', 'wpgetapi' ); ?></li>
467 <li><?php esc_html_e( 'Set the Method.', 'wpgetapi' ); ?></li>
468 <li><?php esc_html_e( 'Set the desired Results Format.', 'wpgetapi' ); ?></li>
469 <li><?php esc_html_e( 'The rest of the fields are optional and will depend on the API you using.', 'wpgetapi' ); ?></li>
470 <li><?php esc_html_e( 'Hit the Save button.', 'wpgetapi' ); ?></li>
471 <li><?php esc_html_e( 'After saving, copy the Template Tag or the Shortcode and paste into appropriate place.', 'wpgetapi' ); ?></li>
472 </ol>
473 </div>
474
475 <hr>
476
477 <div class="box">
478 <strong><?php esc_html_e( 'Notes', 'wpgetapi' ); ?></strong>
479 <ul>
480 <li><?php esc_html_e( 'The Unique ID can be whatever you like. This is used to identify each endpoint within the plugin.', 'wpgetapi' ); ?></li>
481 <li><?php esc_html_e( 'The Endpoint can be found in the documentation of your API.', 'wpgetapi' ); ?></li>
482 </ul>
483 </div>
484
485 <hr>
486
487 <div class="box">
488 <strong><?php esc_html_e( 'Getting Help', 'wpgetapi' ); ?></strong>
489 <ul>
490 <li><?php
491 printf(
492 esc_html__( 'Visit our website for guides on %1$s or %2$s.', 'cmb2' ),
493 '<a target="_blank" href="https://wpgetapi.com/docs/using-the-shortcode/?utm_campaign=Shortcode&utm_medium=Admin&utm_source=User">Using the Shortcode</a>',
494 '<a target="_blank" href="https://wpgetapi.com/docs/using-the-template-function/?utm_campaign=Template Tag&utm_medium=Admin&utm_source=User">Template Tag</a>'
495 );
496 ?></li>
497 </ul>
498 </div>
499
500 <?php
501 $content = ob_get_contents();
502 ob_end_clean();
503 return $content;
504
505 }
506
507
508 /**
509 * Sidebar for our setup page.
510 * @since 1.4.4
511 */
512 public function sidebar_setup_page() {
513
514 ob_start();
515 ?>
516
517 <div class="box">
518 <h3><?php esc_html_e( 'Setup Instructions', 'wpgetapi' ); ?></h3>
519 <ol>
520 <li><?php esc_html_e( 'Fill in the API Name, Unique ID and Base URL of your API.', 'wpgetapi' ); ?></li>
521 <li><?php esc_html_e( 'Hit the Save button.', 'wpgetapi' ); ?></li>
522 <li><?php esc_html_e( 'After saving, visit the new tab that will be created.', 'wpgetapi' ); ?></li>
523 </ol>
524 </div>
525
526 <hr>
527
528 <div class="box">
529 <strong><?php esc_html_e( 'Notes', 'wpgetapi' ); ?></strong>
530 <ul>
531 <li><?php esc_html_e( 'The API Name and Unique ID can be whatever you like. These are used to identify each API within the plugin.', 'wpgetapi' ); ?></li>
532 <li><?php esc_html_e( 'The Base URL can be found in the documentation of your API.', 'wpgetapi' ); ?></li>
533 </ul>
534 </div>
535
536 <hr>
537
538 <div class="box">
539 <strong><?php esc_html_e( 'Getting Help', 'wpgetapi' ); ?></strong>
540 <ul>
541 <li><?php
542 printf(
543 esc_html__( 'Visit our website to view %1$s or %2$s.', 'cmb2' ),
544 '<a target="_blank" href="https://wpgetapi.com/docs/quick-start-guide/?utm_campaign=Quick Start&utm_medium=Admin&utm_source=User">Quick Start Guide</a>',
545 '<a target="_blank" href="https://wpgetapi.com/docs/step-by-step-example/?utm_campaign=Step by Step&utm_medium=Admin&utm_source=User">Step by Step Example</a>'
546 );
547 ?></li>
548 </ul>
549 </div>
550
551 <?php
552 $content = ob_get_contents();
553 ob_end_clean();
554 return $content;
555
556 }
557
558 /**
559 * Tabs don't immmediately appear on save, so this hack!
560 * @since 0.1.0
561 */
562 public function redirect( $object_id, $updated ) {
563 if( $object_id == 'wpgetapi_setup' ) {
564
565 add_settings_error( 'wpgetapi-notices', '', 'Settings saved, reloading page...', 'updated' );
566 settings_errors( 'wpgetapi-notices' );
567 ?>
568
569 <script>
570 setTimeout(function() {
571 location.reload();
572 }, 1500);
573 </script>
574 <?php
575
576 } else if ( strpos( $object_id, 'wpgetapi_' ) !== false && $object_id != 'wpgetapi_import' ) {
577
578 add_settings_error( 'wpgetapi-notices', '', 'Endpoint settings saved.', 'updated' );
579 settings_errors( 'wpgetapi-notices' );
580
581 }
582 }
583
584
585 /**
586 * Public getter method for retrieving protected/private variables
587 * @since 0.1.0
588 * @param string $field Field to retrieve
589 * @return mixed Field value or exception is thrown
590 */
591 public function __get( $field ) {
592 // Allowed fields to retrieve
593 if ( in_array( $field, array( 'key', 'fields', 'title', 'options_pages' ), true ) ) {
594 return $this->{$field};
595 }
596 if ( 'option_metabox' === $field ) {
597 return $this->option_fields();
598 }
599 throw new Exception( 'Invalid property: ' . $field );
600 }
601
602 }
603
604 /**
605 * Helper function to get/return the WpGetApi_Admin_Options object
606 * @since 0.1.0
607 * @return WpGetApi_Admin_Options object
608 */
609 function wpgetapi_admin_options() {
610 return WpGetApi_Admin_Options::get_instance();
611 }
612
613 // Get it started
614 wpgetapi_admin_options();
615
616 endif;