menu_title = __( 'WPGetAPI', 'wpgetapi' ); $this->title = __( 'WPGetAPI', 'wpgetapi' ); } /** * Returns the running object * * @return Myprefix_Admin **/ public static function get_instance() { if( is_null( self::$instance ) ) { self::$instance = new self(); self::$instance->hooks(); } return self::$instance; } /** * Initiate our hooks * @since 0.1.0 */ public function hooks() { add_action( 'admin_init', array( $this, 'init' ) ); add_action( 'admin_menu', array( $this, 'add_options_pages' ) ); add_action( 'cmb2_admin_init', array( $this, 'init_custom_fields' ) ); add_action( "cmb2_save_options-page_fields", array( $this, 'redirect' ), 1, 2 ); add_action( 'admin_footer', array( $this, 'load_testing_javascript' ) ); add_action( 'wp_ajax_wpgetapi_test_endpoint', array( $this, 'test_the_endpoint' ) ); add_action( 'wp_ajax_wpgetapi_export_endpoints', array( $this, 'export_endpoints' ) ); add_action( 'wp_ajax_wpgetapi_import_endpoints', array( $this, 'import_endpoints' ) ); } /** * Setup our custom fields * @since 0.1.0 */ public function init_custom_fields() { require_once WPGETAPIDIR . 'includes/class-fields.php'; WpGetApi_Parameter_Field::init_parameter(); } /** * Register our setting to WP * @since 0.1.0 */ public function init() { $this->pro_plugin = is_plugin_active( 'wpgetapi-extras/wpgetapi-extras.php' ) ? true : false; $option_tabs = self::option_fields(); foreach ($option_tabs as $index => $option_tab) { register_setting( $option_tab['id'], $option_tab['id'] ); } } /** * Get our saved API's from setup * @since 0.1.0 */ public function get_apis() { $setup = get_option( 'wpgetapi_setup' ); if( empty( $setup ) || ! isset( $setup['apis'] ) || empty( $setup['apis'] ) ) return; return $setup['apis']; } /** * Add the options metabox to the array of metaboxes * @since 0.1.0 */ public function option_fields() { //Only need to initiate the array once per page-load if ( ! empty( $this->option_metabox ) ) { return $this->option_metabox; } // setup tab $option_metabox[] = array( 'title' => __( 'Setup', 'wpgetapi' ), 'menu_title' => __( 'Setup', 'wpgetapi' ), 'id' => 'wpgetapi_setup', 'desc' => __( 'Add the details of the API(s) you are using. Endpoints will be setup in the next step after hitting save.', 'wpgetapi' ), 'show_on' => array( 'key' => 'options-page', 'value' => array( 'wpgetapi_setup' ), ), 'show_names' => true, 'fields' => $this->setup_fields(), ); // get our saved API's and create as tabs if( $this->get_apis() ) { foreach ( $this->get_apis() as $index => $api ) { $name = isset( $api['name'] ) && $api['name'] != '' ? sanitize_text_field( $api['name'] ) : 'API ' . absint( $index ); $api_id = isset( $api['id'] ) && $api['id'] != '' ? sanitize_text_field( $api['id'] ) : $name; $metabox_id = strtolower( str_replace( '-', '_', sanitize_file_name( 'wpgetapi-' . $api_id ) ) ); $base_url = isset( $api['base_url'] ) && $api['base_url'] != '' ? esc_url_raw( $api['base_url'], array( 'http', 'https' ) ) : ''; $type = isset( $api['auth_type'] ) && $api['auth_type'] != '' ? sanitize_text_field( $api['auth_type'] ) : ''; // tab $option_metabox[] = array( 'title' => $name, 'menu_title' => $name, 'id' => $metabox_id, 'desc' => '', 'show_on' => array( 'key' => 'options-page', 'value' => array( $metabox_id ), ), 'show_names' => true, 'fields' => $this->endpoint_fields( $type, $api_id, $base_url ), ); } } $option_metabox = apply_filters( 'wpgetapi_admin_pages', $option_metabox ); return $option_metabox; } public function setup_fields() { $fields[] = array( 'id' => 'apis', 'type' => 'group', 'name' => '', 'description' => '', 'options' => array( 'group_title' => __( 'API {#}', 'wpgetapi' ), 'add_button' => __( 'Add New API', 'wpgetapi' ), 'remove_button' => __( 'Remove API', 'wpgetapi' ), 'sortable' => true, // beta ), 'fields' => array( array( 'name' => __( 'API Name', 'wpgetapi' ) . '', 'id' => 'name', 'type' => 'text', 'attributes' => array( 'required' => true, 'placeholder' => 'Google Maps', ), 'desc' => '', ), array( 'name' => __( 'Unique ID', 'wpgetapi' ) . '', 'id' => 'id', 'type' => 'text', 'attributes' => array( 'required' => true, 'placeholder' => 'google_maps', ), 'desc' => '', 'sanitization_cb' => 'wpgetapi_sanitize_unique_id' ), array( 'name' => __( 'Base URL', 'wpgetapi' ) . '', 'id' => 'base_url', 'type' => 'text', 'attributes' => array( 'required' => true, 'placeholder' => 'https://maps.googleapis.com/maps/api', ), 'desc' => '', ), ) ); return $fields; } /** * Endpoint settings. * @return sale info array * */ public function endpoint_fields( $type = '', $api_id = '', $base_url = '' ) { $fields = array(); $cache_disabled = $this->pro_plugin ? '' : 'disabled'; $cache_url = $this->pro_plugin ? 'Caching Help' : 'View PRO Plugin'; $cache_desc = $this->pro_plugin ? '' : 'Available in PRO plugin.'; $endpoint_fields = apply_filters( 'wpgetapi_fields_endpoints', array( array( 'name' => __( 'Unique ID', 'wpgetapi' ) . '', 'id' => 'id', 'type' => 'text', 'classes' => 'field-id', 'attributes' => array( 'required' => true, 'placeholder' => 'new_endpoint', ), 'desc' => '', 'before_row' => "wpgetapi_output_top_of_endpoint", 'api_id' => $api_id, 'sanitization_cb' => 'wpgetapi_sanitize_unique_id' ), array( 'name' => __( 'Endpoint', 'wpgetapi' ) . '', 'id' => 'endpoint', 'type' => 'text', 'classes' => 'field-endpoint', 'attributes' => array( 'required' => true, 'placeholder' => '/newendpoint', ), 'desc' => '', ), array( 'name' => __( 'Method', 'wpgetapi' ) . '', 'id' => 'method', 'type' => 'select', 'classes' => 'field-method', 'attributes' => array( 'required' => true, ), 'options' => array( 'GET' => 'GET', 'POST' => 'POST', 'PUT' => 'PUT', 'PATCH' => 'PATCH', 'DELETE' => 'DELETE', ), 'desc' => '', ), array( 'name' => __( 'Results Format', 'wpgetapi' ) . 'Format as HTML' ) ) . '">', 'id' => 'results_format', 'type' => 'select', 'classes' => 'field-results-format', 'attributes' => array( 'required' => true, ), 'options_cb' => 'wpgetapi_results_format_options', 'desc' => '' ), array( 'name' => __( 'Cache Time', 'wpgetapi' ) . '', 'id' => 'cache_time', 'type' => 'text', 'classes' => 'field-cache-time', 'attributes' => array( 'placeholder' => '3600', $cache_disabled => $cache_disabled, ), 'desc' => $cache_desc ), array( 'name' => __( 'Query String', 'wpgetapi' ) . 'More Info' ) ) . '">', 'id' => 'query_parameters', 'type' => 'parameter', 'classes' => 'field-query-parameters', 'repeatable' => true, 'desc' => '' ), array( 'name' => __( 'Headers', 'wpgetapi' ) . 'More Info' ) ) . '">', 'id' => 'header_parameters', 'type' => 'parameter', 'classes' => 'field-header-parameters', 'repeatable' => true, 'desc' => '' ), array( 'name' => __( 'Body POST Fields', 'wpgetapi' ) . 'More Info' ) ) . '">', 'id' => 'body_parameters', 'type' => 'parameter', 'classes' => 'field-body-parameters', 'repeatable' => true, 'desc' => '' ), array( 'name' => __( 'Encode Body', 'wpgetapi' ) . '', 'id' => 'body_json_encode', 'type' => 'select', 'classes' => 'field-body-json-encode', 'options' => array( 'false' => __( 'No encoding (raw)', 'wpgetapi' ), 'true' => __( 'JSON encode', 'wpgetapi' ), 'url' => __( 'URL encode (x-www-form-urlencoded)', 'wpgetapi' ), 'base64' => __( 'Base64 encode', 'wpgetapi' ), 'xml' => __( 'XML format (available in PRO)', 'wpgetapi' ), ), 'desc' => '', ), array( 'name' => __( 'Actions', 'wpgetapi' ) . 'View PRO Plugin' ) ) . '">', 'id' => 'actions', 'type' => 'select', 'classes' => 'field-actions', 'desc' => __( 'Available in PRO plugin.', 'wpgetapi' ), 'options' => array( '' => __( '-- No Action --', 'wpgetapi-extras' ), 'new_post_published' => __( 'Post/Custom Post - New Post Published', 'wpgetapi-extras' ), 'transition_post_status' => __( 'Post/Custom Post - Status Changed', 'wpgetapi-extras' ), 'delete_post' => __( 'Post/Custom Post - Delete Post', 'wpgetapi-extras' ), 'user_register' => __( 'User - New User Registered', 'wpgetapi-extras' ), 'delete_user' => __( 'User - Delete User', 'wpgetapi-extras' ), 'user_login' => __( 'User - User Logs In', 'wpgetapi-extras' ), 'woocommerce_new_product' => __( 'WooCommerce - New Product Created', 'wpgetapi-extras' ), 'woocommerce_new_order' => __( 'WooCommerce - New Order Created', 'wpgetapi-extras' ), 'woocommerce_order_status_changed' => __( 'WooCommerce - Order Status Changed', 'wpgetapi-extras' ), 'contact_form_7' => __( 'Contact Form 7', 'wpgetapi-extras' ), 'gravity_forms' => __( 'Gravity Forms', 'wpgetapi-extras' ), 'wpforms' => __( 'WPForms', 'wpgetapi-extras' ), 'jetformbuilder' => __( 'JetFormBuilder', 'wpgetapi-extras' ), 'formidable_forms' => __( 'Formidable Forms', 'wpgetapi-extras' ), 'pmp' => __( 'Paid Memberships Pro - After Checkout', 'wpgetapi-extras' ), 'edd' => __( 'Easy Digital Downloads - Complete Purchase', 'wpgetapi-extras' ), ), 'default' => '', ) ) ); $fields[] = array( 'id' => 'endpoints', 'type' => 'group', 'name' => '', 'description' => '', 'options' => array( 'group_title' => __( 'Endpoint {#}', 'wpgetapi' ), 'add_button' => __( 'Add Endpoint', 'wpgetapi' ), 'remove_button' => __( 'Remove Endpoint', 'wpgetapi' ), 'sortable' => true, // beta //'closed' => true, ), 'before_group' => '
', 'fields' => $endpoint_fields, ); return apply_filters( 'wpgetapi_fields', $fields ); } public function add_options_pages() { add_thickbox(); $option_tabs = self::option_fields(); foreach ($option_tabs as $index => $option_tab) { if ( $index == 0) { $this->options_pages[] = add_menu_page( $this->title, $this->menu_title, 'manage_options', $option_tab['id'], array( $this, 'admin_page_display' ), 'none' ); //Link admin menu to first tab 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 } else { $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' ) ); } } foreach ( $this->options_pages as $page ) { // Include CMB CSS in the head to avoid FOUC add_action( "admin_print_styles-{$page}", array( 'CMB2_Hookup', 'enqueue_cmb_css' ) ); } } /** * Admin page markup. Mmply handled by CMB2 * @since 0.1.0 */ public function admin_page_display() { $option_tabs = self::option_fields(); //get all option tabs $tab_forms = array(); ?> true ) ); $output = array( 'data' => $data, 'endpoint_id' => $endpoint_id ); echo json_encode( $output ); wp_die(); // this is required to terminate immediately and return a proper response } public function get_import_export() { ob_start(); ?>Base URL:' . $base_url . '
Export the endpoints for this API.
After clicking the button below, copy the entire contents of the export.
Paste your exported JSON data into the field below to import endpoints into this API.