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

620 lines 18.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 * 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' => 'https://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 'PUT' => 'PUT',
281 ),
282 'desc' => __( 'The request method for this endpoint.', 'wpgetapi' ),
283 ),
284 array(
285 'name' => __( 'Results Format', 'wpgetapi' ),
286 'id' => 'results_format',
287 'type' => 'select',
288 'classes' => 'field-results-format',
289 'attributes' => array(
290 'required' => true,
291 ),
292 'options_cb' => 'wpgetapi_results_format_options',
293 'desc' => __( 'The format of the results.', 'wpgetapi' ),
294 ),
295 array(
296 'name' => __( 'Query String', 'wpgetapi' ),
297 'id' => 'query_parameters',
298 'type' => 'parameter',
299 'classes' => 'field-query-parameters',
300 'repeatable' => true,
301 'desc' => sprintf(
302 __( 'Parameters as name/value pairs that will be appended to the URL. %1s', 'wpgetapi' ),
303 '<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>'
304 )
305 ),
306 array(
307 'name' => __( 'Headers', 'wpgetapi' ),
308 'id' => 'header_parameters',
309 'type' => 'parameter',
310 'classes' => 'field-header-parameters',
311 'repeatable' => true,
312 'desc' => sprintf(
313 __( 'Parameters as name/value pairs, sent in the headers. %1s', 'wpgetapi' ),
314 '<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>'
315 )
316 ),
317 array(
318 'name' => __( 'Body POST Fields', 'wpgetapi' ),
319 'id' => 'body_parameters',
320 'type' => 'parameter',
321 'classes' => 'field-body-parameters',
322 'repeatable' => true,
323 'desc' => sprintf(
324 __( 'Parameters as name/value pairs, sent in the body as POST fields. %1s', 'wpgetapi' ),
325 '<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>'
326 )
327 ),
328 array(
329 'name' => __( 'Encode Body', 'wpgetapi' ),
330 'id' => 'body_json_encode',
331 'type' => 'select',
332 'classes' => 'field-body-json-encode',
333 'options' => array(
334 'false' => __( 'No encoding', 'wpgetapi' ),
335 'true' => __( 'JSON encode', 'wpgetapi' ),
336 'url' => __( 'URL encode', 'wpgetapi' ),
337 ),
338 'desc' => __( 'Should Body parameters be encoded (if set). ', 'wpgetapi' ),
339 ),
340 ) );
341
342
343 $fields[] = array(
344 'id' => 'endpoints',
345 'type' => 'group',
346 'name' => '',
347 'description' => '',
348 'options' => array(
349 'group_title' => __( 'Endpoint {#}', 'wpgetapi' ),
350 'add_button' => __( 'Add Endpoint', 'wpgetapi' ),
351 'remove_button' => __( 'Remove Endpoint', 'wpgetapi' ),
352 'sortable' => true, // beta
353 ),
354 'before_group' => '<pre class="url">Base URL: <span>' . $base_url . '</span></pre>',
355 'fields' => $endpoint_fields,
356 );
357
358 return apply_filters( 'wpgetapi_fields', $fields );
359
360 }
361
362
363 public function add_options_pages() {
364
365 $option_tabs = self::option_fields();
366
367 foreach ($option_tabs as $index => $option_tab) {
368 if ( $index == 0) {
369
370 $this->options_pages[] = add_menu_page( $this->title, $this->menu_title, 'manage_options', $option_tab['id'], array( $this, 'admin_page_display' ), 'dashicons-editor-code'
371 ); //Link admin menu to first tab
372
373 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
374 } else {
375 $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' ) );
376 }
377 }
378
379 foreach ( $this->options_pages as $page ) {
380 // Include CMB CSS in the head to avoid FOUC
381 add_action( "admin_print_styles-{$page}", array( 'CMB2_Hookup', 'enqueue_cmb_css' ) );
382 }
383
384 }
385
386 /**
387 * Admin page markup. Mmply handled by CMB2
388 * @since 0.1.0
389 */
390 public function admin_page_display() {
391
392 $option_tabs = self::option_fields(); //get all option tabs
393 $tab_forms = array();
394 ?>
395
396 <div class="wrap wpgetapi">
397
398 <div class="main_content_cell">
399
400 <h1 class="wp-heading-inline"><?php esc_html_e( $this->title, 'wpgetapi' ) ?></h1>
401 <!-- Options Page Nav Tabs -->
402 <h2 class="nav-tab-wrapper">
403 <?php foreach ($option_tabs as $option_tab) :
404 $tab_slug = $option_tab['id'];
405 $nav_class = 'nav-tab';
406 if ( $tab_slug == $_GET['page'] ) {
407 $nav_class .= ' nav-tab-active'; //add active class to current tab
408 $tab_forms[] = $option_tab; //add current tab to forms to be rendered
409 }
410 ?>
411 <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>
412 <?php endforeach; ?>
413 </h2>
414 <!-- End of Nav Tabs -->
415
416 <?php foreach ($tab_forms as $tab_form) : //render all tab forms (normaly just 1 form)
417
418 ?>
419
420 <div id="<?php esc_attr_e($tab_form['id']); ?>" class="cmb-form group">
421 <div class="metabox-holder">
422 <div class="pmpbox pad">
423 <h3 class="title"><?php esc_html_e($tab_form['title'], 'wpgetapi'); ?></h3>
424 <div class="desc"><?php echo wp_kses_post( $tab_form['desc'] ); ?></div>
425 <?php cmb2_metabox_form( $tab_form, $tab_form['id'], $tab_form ); ?>
426 </div>
427 </div>
428 </div>
429
430 <?php endforeach; ?>
431
432 </div>
433
434 <div class="sidebar_cell">
435
436 <?php
437 $sidebar = self::sidebar_display();
438 echo apply_filters( 'wpgetapi_admin_sidebar_display', $sidebar );
439 ?>
440
441 </div>
442
443 </div>
444
445 <?php
446
447 }
448
449
450 /**
451 * Sidebar markup.
452 * @since 1.4.1
453 */
454 public function sidebar_display() {
455
456 ob_start();
457
458 // do our setup page
459 if( isset( $_GET['page'] ) && $_GET['page'] === 'wpgetapi_setup' )
460 return $this->sidebar_setup_page();
461
462 ?>
463
464 <div class="box">
465 <h3><?php esc_html_e( 'Endpoint Instructions', 'wpgetapi' ); ?></h3>
466 <ol>
467 <li><?php esc_html_e( 'Fill in the Unique ID and Endpoint.', 'wpgetapi' ); ?></li>
468 <li><?php esc_html_e( 'Set the Method.', 'wpgetapi' ); ?></li>
469 <li><?php esc_html_e( 'Set the desired Results Format.', 'wpgetapi' ); ?></li>
470 <li><?php esc_html_e( 'The rest of the fields are optional and will depend on the API you using.', 'wpgetapi' ); ?></li>
471 <li><?php esc_html_e( 'Hit the Save button.', 'wpgetapi' ); ?></li>
472 <li><?php esc_html_e( 'After saving, copy the Template Tag or the Shortcode and paste into appropriate place.', 'wpgetapi' ); ?></li>
473 </ol>
474 </div>
475
476 <hr>
477
478 <div class="box">
479 <strong><?php esc_html_e( 'Notes', 'wpgetapi' ); ?></strong>
480 <ul>
481 <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>
482 <li><?php esc_html_e( 'The Endpoint can be found in the documentation of your API.', 'wpgetapi' ); ?></li>
483 </ul>
484 </div>
485
486 <hr>
487
488 <div class="box">
489 <strong><?php esc_html_e( 'Getting Help', 'wpgetapi' ); ?></strong>
490 <ul>
491 <li><?php
492 printf(
493 esc_html__( 'Visit our website for guides on %1$s or %2$s.', 'cmb2' ),
494 '<a target="_blank" href="https://wpgetapi.com/docs/using-the-shortcode/?utm_campaign=Shortcode&utm_medium=Admin&utm_source=User">Using the Shortcode</a>',
495 '<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>'
496 );
497 ?></li>
498 </ul>
499 </div>
500
501 <?php
502 $content = ob_get_contents();
503 ob_end_clean();
504 return $content;
505
506 }
507
508
509 /**
510 * Sidebar for our setup page.
511 * @since 1.4.4
512 */
513 public function sidebar_setup_page() {
514
515 ob_start();
516 ?>
517
518 <div class="box">
519 <h3><?php esc_html_e( 'Setup Instructions', 'wpgetapi' ); ?></h3>
520 <ol>
521 <li><?php esc_html_e( 'Fill in the API Name, Unique ID and Base URL of your API.', 'wpgetapi' ); ?></li>
522 <li><?php esc_html_e( 'Hit the Save button.', 'wpgetapi' ); ?></li>
523 <li><?php esc_html_e( 'After saving, visit the new tab that will be created.', 'wpgetapi' ); ?></li>
524 </ol>
525 </div>
526
527 <hr>
528
529 <div class="box">
530 <strong><?php esc_html_e( 'Notes', 'wpgetapi' ); ?></strong>
531 <ul>
532 <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>
533 <li><?php esc_html_e( 'The Base URL can be found in the documentation of your API.', 'wpgetapi' ); ?></li>
534 </ul>
535 </div>
536
537 <hr>
538
539 <div class="box">
540 <strong><?php esc_html_e( 'Getting Help', 'wpgetapi' ); ?></strong>
541 <ul>
542 <li><?php
543 printf(
544 esc_html__( 'Visit our website to view %1$s or %2$s.', 'cmb2' ),
545 '<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>',
546 '<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>'
547 );
548 ?></li>
549 </ul>
550 </div>
551
552 <?php
553 $content = ob_get_contents();
554 ob_end_clean();
555 return $content;
556
557 }
558
559 /**
560 * Tabs don't immmediately appear on save, so this hack!
561 * @since 0.1.0
562 */
563 public function redirect( $object_id, $updated ) {
564
565 if( $object_id == 'wpgetapi_setup' ) {
566
567 add_settings_error( 'wpgetapi-notices', '', 'Settings saved, reloading page...', 'updated' );
568 settings_errors( 'wpgetapi-notices' );
569 ?>
570
571 <script>
572 setTimeout(function() {
573 location.reload();
574 }, 1000);
575 </script>
576 <?php
577
578 } else if ( strpos( $object_id, 'wpgetapi_' ) !== false ) {
579
580 $text = apply_filters( 'wpgetapi_admin_notice_text', __( 'Settings saved.', 'wpgetapi' ), $object_id );
581 add_settings_error( 'wpgetapi-notices', '', $text, 'updated' );
582 settings_errors( 'wpgetapi-notices' );
583
584 }
585
586 }
587
588
589 /**
590 * Public getter method for retrieving protected/private variables
591 * @since 0.1.0
592 * @param string $field Field to retrieve
593 * @return mixed Field value or exception is thrown
594 */
595 public function __get( $field ) {
596 // Allowed fields to retrieve
597 if ( in_array( $field, array( 'key', 'fields', 'title', 'options_pages' ), true ) ) {
598 return $this->{$field};
599 }
600 if ( 'option_metabox' === $field ) {
601 return $this->option_fields();
602 }
603 throw new Exception( 'Invalid property: ' . $field );
604 }
605
606 }
607
608 /**
609 * Helper function to get/return the WpGetApi_Admin_Options object
610 * @since 0.1.0
611 * @return WpGetApi_Admin_Options object
612 */
613 function wpgetapi_admin_options() {
614 return WpGetApi_Admin_Options::get_instance();
615 }
616
617 // Get it started
618 wpgetapi_admin_options();
619
620 endif;