PluginProbe
WPGet API – Connect to any external REST API / 1.8.3
WPGet API – Connect to any external REST API v1.8.3
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 / frontend / functions.php

functions.php in WPGet API – Connect to any external REST API 1.8.3, at frontend/functions.php

123 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Return the endpoint data
5 */
6 function wpgetapi_endpoint( $api_id = '', $endpoint_id = '', $args = array(), $keys = array() ) {
7
8 if( ! $api_id || ! $endpoint_id )
9 return false;
10
11 $api = new WpGetApi_Api( $api_id, $endpoint_id, $args, $keys );
12
13 return $api->endpoint_output();
14
15 }
16
17 /**
18 * Return the endpoint data via shortcode
19 */
20 function wpgetapi_endpoint_shortcode( $atts = array() ) {
21
22 $a = shortcode_atts( array(
23 'api_id' => '',
24 'endpoint_id' => '',
25 'debug' => false,
26 'args' => array(),
27 'keys' => array(),
28 'endpoint_variables' => array(),
29 'query_variables' => array(),
30 'format' => '',
31 'html_tag' => 'div',
32 'html_labels' => 'false',
33 'html_url' => '',
34 'html_to_link' => '',
35 'on' => '',
36 'button_text' => '',
37 ), $atts );
38
39 if( ! isset( $a['api_id'] ) || $a['api_id'] == '' )
40 return __( 'api_id shortcode attribute is not set.', 'wpgetapi' );
41
42 if( ! isset( $a['endpoint_id'] ) || $a['endpoint_id'] == '' )
43 return __( 'endpoint_id shortcode attribute is not set.', 'wpgetapi' );
44
45
46
47 // sort out our keys if using them
48 if( ! empty( $a['keys'] ) ) {
49 // Create our array of values for keys
50 // First, sanitize the data and remove white spaces
51 $no_whitespaces_keys = preg_replace( '/\s*,\s*/', ',', filter_var( $a['keys'], FILTER_SANITIZE_STRING ) );
52 $a['keys'] = explode( ',', $no_whitespaces_keys );
53 }
54
55 // sort out our endpoint_variables if using them
56 if( ! empty( $a['endpoint_variables'] ) ) {
57 // Create our array of values for endpoint_variables
58 // First, sanitize the data and remove white spaces
59 $no_whitespaces_vars = preg_replace( '/\s*,\s*/', ',', filter_var( $a['endpoint_variables'], FILTER_SANITIZE_STRING ) );
60 $a['endpoint_variables'] = explode( ',', $no_whitespaces_vars );
61 }
62
63 // add our shortcode args to the actual 'args' within the endpoint call
64 $a['args']['debug'] = $a['debug'];
65 $a['args']['endpoint_variables'] = $a['endpoint_variables'];
66 $a['args']['query_variables'] = $a['query_variables'];
67 $a['args']['format'] = $a['format'];
68 $a['args']['html_tag'] = $a['html_tag'];
69 $a['args']['html_labels'] = $a['html_labels'];
70 $a['args']['html_url'] = $a['html_url'];
71 $a['args']['html_to_link'] = $a['html_to_link'];
72 $a['args']['shortcode'] = true;
73 $a['args']['on'] = $a['on'];
74 $a['args']['button_text'] = $a['button_text'];
75
76 if( isset( $a['args']['on'] ) && $a['args']['on'] == 'ajax' && ! wp_doing_ajax() ) {
77
78 if( class_exists( 'WpGetApi_Extras_Extend' ) ) {
79
80 add_action( 'wp_footer', array( 'WpGetApi_Extras_Extend', 'ajax_function' ) );
81
82 $button_text = isset( $a['args']['button_text'] ) && $a['args']['button_text'] != '' ? $a['args']['button_text'] : 'Call API';
83
84 ob_start(); ?>
85
86 <button
87 class="button btn btn-primary wpgetapi_ajax_request"
88 data-api="<?php esc_html_e( $a['api_id'] ); ?>"
89 data-endpoint="<?php esc_html_e( $a['endpoint_id'] ); ?>"
90 data-args="<?php esc_html_e( json_encode( $a['args'] ) ); ?>"
91 data-keys="<?php esc_html_e( json_encode( $a['keys'] ) ); ?>">
92 <?php esc_html_e( $button_text ); ?>
93 </button>
94 <div class="wpgetapi_ajax_output"></div>
95
96 <?php
97 $result = ob_get_contents();
98 ob_end_clean();
99
100 } else {
101
102 $result = 'You need the <a href="https://wpgetapi.com/downloads/pro-plugin">PRO plugin</a> for this AJAX functionality.';
103
104 }
105
106 } else {
107
108 $result = wpgetapi_endpoint( $a['api_id'], $a['endpoint_id'], $a['args'], $a['keys'] );
109
110 }
111
112 if( is_array( $result ) ) {
113 $result = sprintf(
114 __( 'The <b>Results Format</b> for this endpoint is set to \'PHP array data\' - shortcodes are unable to output this type of data.<br>Please change the Results Format to \'JSON string\' in the endpoint settings page.<br>Alternatively, please see our tutorial on %1s.', 'wpgetapi' ),
115 '<a target="_blank" href="https://wpgetapi.com/docs/format-api-data-as-html/?utm_campaign=Shortcode JSON&utm_medium=Admin&utm_source=User">how to format your API data as HTML</a>'
116 );
117 }
118
119 return $result;
120
121 }
122 add_shortcode( 'wpgetapi_endpoint', 'wpgetapi_endpoint_shortcode' );
123