| 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_init', array( $this, 'init_custom_fields' ) ); |
| 87 |
} |
| 88 |
|
| 89 |
public function init_custom_fields() { |
| 90 |
require_once WPGETAPIDIR . 'includes/class-fields.php'; |
| 91 |
WpGetApi_Parameter_Field::init_parameter(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Register our setting to WP |
| 96 |
* @since 0.1.0 |
| 97 |
*/ |
| 98 |
public function init() { |
| 99 |
$option_tabs = self::option_fields(); |
| 100 |
foreach ($option_tabs as $index => $option_tab) { |
| 101 |
register_setting( $option_tab['id'], $option_tab['id'] ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Get our saved API's from setup |
| 108 |
* @since 0.1.0 |
| 109 |
*/ |
| 110 |
public function get_apis() { |
| 111 |
|
| 112 |
$setup = get_option( 'wpgetapi_setup' ); |
| 113 |
|
| 114 |
if( empty( $setup ) || ! isset( $setup['apis'] ) || empty( $setup['apis'] ) ) |
| 115 |
return; |
| 116 |
|
| 117 |
return $setup['apis']; |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Add the options metabox to the array of metaboxes |
| 124 |
* @since 0.1.0 |
| 125 |
*/ |
| 126 |
public function option_fields() { |
| 127 |
|
| 128 |
//Only need to initiate the array once per page-load |
| 129 |
if ( ! empty( $this->option_metabox ) ) { |
| 130 |
return $this->option_metabox; |
| 131 |
} |
| 132 |
|
| 133 |
// sale tab |
| 134 |
$option_metabox[] = array( |
| 135 |
'title' => __( 'Setup', 'wpgetapi' ), |
| 136 |
'menu_title' => __( 'Setup', 'wpgetapi' ), |
| 137 |
'id' => 'wpgetapi_setup', |
| 138 |
'desc' => __( 'Add the details of your API(s) below and hit the Save button.', 'wpgetapi' ), |
| 139 |
'show_on' => array( 'key' => 'options-page', 'value' => array( 'setup' ), ), |
| 140 |
'show_names' => true, |
| 141 |
'fields' => $this->setup_fields(), |
| 142 |
); |
| 143 |
|
| 144 |
// get our saved API's and create as tabs |
| 145 |
if( $this->get_apis() ) { |
| 146 |
|
| 147 |
foreach ( $this->get_apis() as $index => $api ) { |
| 148 |
|
| 149 |
$name = isset( $api['name'] ) && $api['name'] != '' ? sanitize_text_field( $api['name'] ) : 'API ' . absint( $index ); |
| 150 |
$api_id = isset( $api['id'] ) && $api['id'] != '' ? sanitize_text_field( $api['id'] ) : $name; |
| 151 |
$metabox_id = strtolower( str_replace( '-', '_', sanitize_file_name( 'wpgetapi-' . $api_id ) ) ); |
| 152 |
$base_url = isset( $api['base_url'] ) && $api['base_url'] != '' ? esc_url_raw( $api['base_url'], array( 'http', 'https' ) ) : ''; |
| 153 |
$type = isset( $api['auth_type'] ) && $api['auth_type'] != '' ? sanitize_text_field( $api['auth_type'] ) : ''; |
| 154 |
|
| 155 |
// tab |
| 156 |
$option_metabox[] = array( |
| 157 |
'title' => $name, |
| 158 |
'menu_title' => $name, |
| 159 |
'id' => $metabox_id, |
| 160 |
'desc' => '', |
| 161 |
'show_on' => array( 'key' => 'options-page', 'value' => array( $metabox_id ), ), |
| 162 |
'show_names' => true, |
| 163 |
'fields' => $this->api_fields( $type, $api_id, $base_url ), |
| 164 |
); |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
return $option_metabox; |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
public function setup_fields() { |
| 176 |
|
| 177 |
$fields[] = array( |
| 178 |
'id' => 'apis', |
| 179 |
'type' => 'group', |
| 180 |
'name' => '', |
| 181 |
'description' => '', |
| 182 |
'options' => array( |
| 183 |
'group_title' => __( 'API {#}', 'wpgetapi' ), |
| 184 |
'add_button' => __( 'Add API', 'wpgetapi' ), |
| 185 |
'remove_button' => __( 'Remove API', 'wpgetapi' ), |
| 186 |
'sortable' => true, // beta |
| 187 |
), |
| 188 |
'fields' => array( |
| 189 |
array( |
| 190 |
'name' => __( 'API Name', 'wpgetapi' ), |
| 191 |
'id' => 'name', |
| 192 |
'type' => 'text', |
| 193 |
'attributes' => array( |
| 194 |
'required' => true, |
| 195 |
'placeholder' => 'New API Name', |
| 196 |
), |
| 197 |
'desc' => __( 'The name of the API you are connecting to.', 'wpgetapi' ), |
| 198 |
), |
| 199 |
array( |
| 200 |
'name' => __( 'API ID', 'wpgetapi' ), |
| 201 |
'id' => 'id', |
| 202 |
'type' => 'text', |
| 203 |
'attributes' => array( |
| 204 |
'required' => true, |
| 205 |
'placeholder' => 'new_api_name', |
| 206 |
), |
| 207 |
'desc' => __( 'A unique ID for your API. Once this is set you should not change this value.', 'wpgetapi' ), |
| 208 |
), |
| 209 |
array( |
| 210 |
'name' => __( 'Base URL', 'wpgetapi' ), |
| 211 |
'id' => 'base_url', |
| 212 |
'type' => 'text', |
| 213 |
'attributes' => array( |
| 214 |
'required' => true, |
| 215 |
'placeholder' => 'https://newapiurl.com', |
| 216 |
), |
| 217 |
'desc' => __( 'The base URL of the API you are connecting to.', 'wpgetapi' ), |
| 218 |
), |
| 219 |
|
| 220 |
) |
| 221 |
); |
| 222 |
|
| 223 |
return $fields; |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
/** |
| 229 |
* Site wide info on the current sale. |
| 230 |
* @return sale info array |
| 231 |
* |
| 232 |
*/ |
| 233 |
public function api_fields( $type = '', $api_id = '', $base_url = '' ) { |
| 234 |
|
| 235 |
$fields = array(); |
| 236 |
|
| 237 |
$fields[] = array( |
| 238 |
'name' => __( 'Timeout', 'wpgetapi' ), |
| 239 |
'id' => 'timeout', |
| 240 |
'type' => 'text', |
| 241 |
'desc' => __( 'Timeout of the API call.', 'wpgetapi' ), |
| 242 |
'attributes' => array( |
| 243 |
'placeholder' => '10' |
| 244 |
), |
| 245 |
'before_row' => '<pre class="url"><span>Base URL: </span>' . $base_url . '</pre>', |
| 246 |
); |
| 247 |
$fields[] = array( |
| 248 |
'name' => __( 'SSL Verify', 'wpgetapi' ), |
| 249 |
'id' => 'sslverify', |
| 250 |
'type' => 'select', |
| 251 |
'desc' => __( 'This should normally be set to true. If you are having issues, you can try false.', 'wpgetapi' ), |
| 252 |
'options' => array( |
| 253 |
'1' => 'True', |
| 254 |
'0' => 'False' |
| 255 |
), |
| 256 |
); |
| 257 |
|
| 258 |
$endpoint_fields = apply_filters( 'wpgetapi_fields_endpoints', array( |
| 259 |
array( |
| 260 |
'name' => __( 'Unique ID', 'wpgetapi' ), |
| 261 |
'id' => 'id', |
| 262 |
'type' => 'text', |
| 263 |
'classes' => 'field-id', |
| 264 |
'attributes' => array( |
| 265 |
'required' => true, |
| 266 |
'placeholder' => 'new_endpoint', |
| 267 |
), |
| 268 |
'desc' => __( 'Choose a unique ID for this endpoint. Once this is set you should not change this value.', 'wpgetapi' ), |
| 269 |
'before_row' => " |
| 270 |
<pre class='functions'> |
| 271 |
Template Function: </span><span>wpgetapi_endpoint( '" . $api_id . "', '<span class='endpoint_id'></span>', array('debug' => false) );</span><br> |
| 272 |
Shortcode: <span>[wpgetapi_endpoint api_id='" . $api_id . "' endpoint_id='<span class='endpoint_id'></span>' debug='false']</span> |
| 273 |
</pre> |
| 274 |
", |
| 275 |
), |
| 276 |
array( |
| 277 |
'name' => __( 'Endpoint', 'wpgetapi' ), |
| 278 |
'id' => 'endpoint', |
| 279 |
'type' => 'text', |
| 280 |
'classes' => 'field-endpoint', |
| 281 |
'attributes' => array( |
| 282 |
'required' => true, |
| 283 |
'placeholder' => '/newendpoint', |
| 284 |
), |
| 285 |
'desc' => __( 'The endpoint that will be appended to the base URL to get the data. Include slash at beginning.', 'wpgetapi' ), |
| 286 |
), |
| 287 |
array( |
| 288 |
'name' => __( 'Method', 'wpgetapi' ), |
| 289 |
'id' => 'method', |
| 290 |
'type' => 'select', |
| 291 |
'classes' => 'field-method', |
| 292 |
'attributes' => array( |
| 293 |
'required' => true, |
| 294 |
), |
| 295 |
'options' => array( |
| 296 |
'GET' => 'GET', |
| 297 |
'POST' => 'POST', |
| 298 |
), |
| 299 |
'desc' => __( 'The request method for this endpoint.', 'wpgetapi' ), |
| 300 |
), |
| 301 |
array( |
| 302 |
'name' => __( 'Results Format', 'wpgetapi' ), |
| 303 |
'id' => 'results_format', |
| 304 |
'type' => 'select', |
| 305 |
'classes' => 'field-results-format', |
| 306 |
'attributes' => array( |
| 307 |
'required' => true, |
| 308 |
), |
| 309 |
'options_cb' => 'wpgetapi_results_format_options', |
| 310 |
'desc' => __( 'The format of the results.', 'wpgetapi' ), |
| 311 |
), |
| 312 |
array( |
| 313 |
'name' => __( 'Query String', 'wpgetapi' ), |
| 314 |
'id' => 'query_parameters', |
| 315 |
'type' => 'parameter', |
| 316 |
'classes' => 'field-query-parameters', |
| 317 |
'repeatable' => true, |
| 318 |
'desc' => __( 'Parameters as name/value pairs that will be appended to the URL. ', 'wpgetapi' ), |
| 319 |
), |
| 320 |
array( |
| 321 |
'name' => __( 'Headers', 'wpgetapi' ), |
| 322 |
'id' => 'header_parameters', |
| 323 |
'type' => 'parameter', |
| 324 |
'classes' => 'field-header-parameters', |
| 325 |
'repeatable' => true, |
| 326 |
'desc' => __( 'Parameters as name/value pairs that will be included in the header. ', 'wpgetapi' ), |
| 327 |
), |
| 328 |
array( |
| 329 |
'name' => __( 'Body', 'wpgetapi' ), |
| 330 |
'id' => 'body_parameters', |
| 331 |
'type' => 'parameter', |
| 332 |
'classes' => 'field-body-parameters', |
| 333 |
'repeatable' => true, |
| 334 |
'desc' => __( 'Parameters as name/value pairs that will be included in the POST body fields. Only used when POST method is selected. ', 'wpgetapi' ), |
| 335 |
), |
| 336 |
array( |
| 337 |
'name' => __( 'JSON Encode Body', 'wpgetapi' ), |
| 338 |
'id' => 'body_json_encode', |
| 339 |
'type' => 'select', |
| 340 |
'classes' => 'field-body-json-encode', |
| 341 |
'options' => array( |
| 342 |
'true' => __( 'Yes', 'wpgetapi' ), |
| 343 |
'false' => __( 'No', 'wpgetapi' ), |
| 344 |
), |
| 345 |
'desc' => __( 'Do you want to JSON encode the Body parameters above. ', 'wpgetapi' ), |
| 346 |
), |
| 347 |
) ); |
| 348 |
|
| 349 |
|
| 350 |
$fields[] = array( |
| 351 |
'id' => 'endpoints', |
| 352 |
'type' => 'group', |
| 353 |
'name' => __( 'Endpoints', 'wpgetapi' ), |
| 354 |
'description' => '', |
| 355 |
'options' => array( |
| 356 |
'group_title' => __( 'Endpoint {#}', 'wpgetapi' ), |
| 357 |
'add_button' => __( 'Add Endpoint', 'wpgetapi' ), |
| 358 |
'remove_button' => __( 'Remove Endpoint', 'wpgetapi' ), |
| 359 |
'sortable' => true, // beta |
| 360 |
), |
| 361 |
'fields' => $endpoint_fields, |
| 362 |
); |
| 363 |
|
| 364 |
return $fields; |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
public function add_options_pages() { |
| 370 |
|
| 371 |
$option_tabs = self::option_fields(); |
| 372 |
|
| 373 |
foreach ($option_tabs as $index => $option_tab) { |
| 374 |
if ( $index == 0) { |
| 375 |
|
| 376 |
$this->options_pages[] = add_menu_page( $this->title, $this->menu_title, 'manage_options', $option_tab['id'], array( $this, 'admin_page_display' ), 'dashicons-editor-code' |
| 377 |
); //Link admin menu to first tab |
| 378 |
|
| 379 |
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 |
| 380 |
} else { |
| 381 |
$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' ) ); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
foreach ( $this->options_pages as $page ) { |
| 386 |
// Include CMB CSS in the head to avoid FOUC |
| 387 |
add_action( "admin_print_styles-{$page}", array( 'CMB2_Hookup', 'enqueue_cmb_css' ) ); |
| 388 |
} |
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Admin page markup. Mmply handled by CMB2 |
| 394 |
* @since 0.1.0 |
| 395 |
*/ |
| 396 |
public function admin_page_display() { |
| 397 |
|
| 398 |
$option_tabs = self::option_fields(); //get all option tabs |
| 399 |
$tab_forms = array(); |
| 400 |
?> |
| 401 |
|
| 402 |
<div class="wrap wpgetapi"> |
| 403 |
|
| 404 |
<div class="main_content_cell"> |
| 405 |
|
| 406 |
<h2><?php esc_html_e( $this->title, 'wpgetapi' ) ?></h2> |
| 407 |
<!-- Options Page Nav Tabs --> |
| 408 |
<h2 class="nav-tab-wrapper"> |
| 409 |
<?php foreach ($option_tabs as $option_tab) : |
| 410 |
$tab_slug = $option_tab['id']; |
| 411 |
$nav_class = 'nav-tab'; |
| 412 |
if ( $tab_slug == $_GET['page'] ) { |
| 413 |
$nav_class .= ' nav-tab-active'; //add active class to current tab |
| 414 |
$tab_forms[] = $option_tab; //add current tab to forms to be rendered |
| 415 |
} |
| 416 |
?> |
| 417 |
<a class="<?php esc_attr_e( $nav_class ); ?>" href="<?php esc_url( menu_page_url( $tab_slug ) ); ?>"><?php esc_attr_e( $option_tab['title'], 'wpgetapi' ); ?></a> |
| 418 |
<?php endforeach; ?> |
| 419 |
</h2> |
| 420 |
<!-- End of Nav Tabs --> |
| 421 |
|
| 422 |
<?php foreach ($tab_forms as $tab_form) : //render all tab forms (normaly just 1 form) ?> |
| 423 |
|
| 424 |
<div id="<?php esc_attr_e($tab_form['id']); ?>" class="cmb-form group"> |
| 425 |
<div class="metabox-holder"> |
| 426 |
<div class="pmpbox pad"> |
| 427 |
<h3 class="title"><?php esc_html_e($tab_form['title'], 'wpgetapi'); ?></h3> |
| 428 |
<div class="desc"><?php echo wp_kses_post( $tab_form['desc'] ); ?></div> |
| 429 |
<?php cmb2_metabox_form( $tab_form, $tab_form['id'] ); ?> |
| 430 |
</div> |
| 431 |
</div> |
| 432 |
</div> |
| 433 |
|
| 434 |
<?php endforeach; ?> |
| 435 |
|
| 436 |
</div> |
| 437 |
|
| 438 |
<div class="sidebar_cell"> |
| 439 |
<div class="box"> |
| 440 |
<h3>Help & Tips</h3> |
| 441 |
<p><a target="_blank" href="https://wpgetapi.com/docs">View The Docs</a></p> |
| 442 |
<p>Using the Template Function within your theme is the most flexible option. It does require basic knowledge of PHP.</p> |
| 443 |
<p>Trying to 'echo' the data when using the 'JSON (as array data)' option will result in a PHP warning. You will need to use 'var_dump' instead to see the output.</p> |
| 444 |
<p>Using the Shortcode within a page (or post) becomes more flexible when used in conjunction with our <a target="_blank" href="https://wpgetapi.com/downloads/pro-plugin">WPGetAPI Pro</a> plugin extension.</p> |
| 445 |
|
| 446 |
<p>You can easily get nested data from the array using our <a target="_blank" href="https://wpgetapi.com/downloads/pro-plugin">WPGetAPI Pro</a> plugin extension using either Shortcode or Template Function.</p> |
| 447 |
|
| 448 |
</div> |
| 449 |
<div class="box"> |
| 450 |
<h3>Contact Us</h3> |
| 451 |
<p>Please <a target="_blank" href="https://wpgetapi.com/">visit our website</a> for any questions, help or feature requests. We are more than happy to help you set up your API.</p> |
| 452 |
</div> |
| 453 |
</div> |
| 454 |
|
| 455 |
</div> |
| 456 |
|
| 457 |
<?php |
| 458 |
|
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Public getter method for retrieving protected/private variables |
| 463 |
* @since 0.1.0 |
| 464 |
* @param string $field Field to retrieve |
| 465 |
* @return mixed Field value or exception is thrown |
| 466 |
*/ |
| 467 |
public function __get( $field ) { |
| 468 |
// Allowed fields to retrieve |
| 469 |
if ( in_array( $field, array( 'key', 'fields', 'title', 'options_pages' ), true ) ) { |
| 470 |
return $this->{$field}; |
| 471 |
} |
| 472 |
if ( 'option_metabox' === $field ) { |
| 473 |
return $this->option_fields(); |
| 474 |
} |
| 475 |
throw new Exception( 'Invalid property: ' . $field ); |
| 476 |
} |
| 477 |
|
| 478 |
} |
| 479 |
|
| 480 |
/** |
| 481 |
* Helper function to get/return the WpGetApi_Admin_Options object |
| 482 |
* @since 0.1.0 |
| 483 |
* @return WpGetApi_Admin_Options object |
| 484 |
*/ |
| 485 |
function wpgetapi_admin_options() { |
| 486 |
return WpGetApi_Admin_Options::get_instance(); |
| 487 |
} |
| 488 |
|
| 489 |
// Get it started |
| 490 |
wpgetapi_admin_options(); |
| 491 |
|
| 492 |
endif; |