PluginProbe
WPGet API – Connect to any external REST API / 1.8.9
WPGet API – Connect to any external REST API v1.8.9
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.9, at frontend/functions.php

158 lines 5.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 'img_key' => '',
36 'img_prepend' => '',
37 'on' => '',
38 'button_text' => '',
39 'hide_button' => '',
40 'button_spinner' => '',
41 'chain' => '',
42 ), $atts );
43
44 if( ! isset( $a['api_id'] ) || $a['api_id'] == '' )
45 return __( 'api_id shortcode attribute is not set.', 'wpgetapi' );
46
47 if( ! isset( $a['endpoint_id'] ) || $a['endpoint_id'] == '' )
48 return __( 'endpoint_id shortcode attribute is not set.', 'wpgetapi' );
49
50
51
52 // sort out our keys if using them
53 if( ! empty( $a['keys'] ) ) {
54 // Create our array of values for keys
55 // First, sanitize the data and remove white spaces
56 $no_whitespaces_keys = preg_replace( '/\s*,\s*/', ',', filter_var( $a['keys'], FILTER_SANITIZE_STRING ) );
57 $a['keys'] = explode( ',', $no_whitespaces_keys );
58 }
59
60 // sort out our endpoint_variables if using them
61 if( ! empty( $a['endpoint_variables'] ) ) {
62 // Create our array of values for endpoint_variables
63 // First, sanitize the data and remove white spaces
64 $no_whitespaces_vars = preg_replace( '/\s*,\s*/', ',', filter_var( $a['endpoint_variables'], FILTER_SANITIZE_STRING ) );
65 $a['endpoint_variables'] = explode( ',', $no_whitespaces_vars );
66 }
67
68 // add our shortcode args to the actual 'args' within the endpoint call
69 $a['args']['debug'] = $a['debug'];
70 $a['args']['endpoint_variables'] = $a['endpoint_variables'];
71 $a['args']['query_variables'] = $a['query_variables'];
72 $a['args']['format'] = $a['format'];
73 $a['args']['html_tag'] = $a['html_tag'];
74 $a['args']['html_labels'] = $a['html_labels'];
75 $a['args']['html_url'] = $a['html_url'];
76 $a['args']['html_to_link'] = $a['html_to_link'];
77 $a['args']['img_key'] = $a['img_key'];
78 $a['args']['img_prepend'] = $a['img_prepend'];
79 $a['args']['shortcode'] = true;
80 $a['args']['on'] = $a['on'];
81 $a['args']['button_text'] = $a['button_text'];
82 $a['args']['hide_button'] = $a['hide_button'];
83 $a['args']['button_spinner'] = $a['button_spinner'];
84 $a['args']['chain'] = $a['chain'];
85
86
87 // if we are doing ajax
88 if( isset( $a['args']['on'] ) && $a['args']['on'] == 'ajax' && ! wp_doing_ajax() ) {
89
90 if( class_exists( 'WpGetApi_Extras_Extend' ) ) {
91
92 add_action( 'wp_footer', array( 'WpGetApi_Extras_Extend', 'ajax_function' ) );
93
94 $button_text = isset( $a['args']['button_text'] ) && $a['args']['button_text'] != '' ? $a['args']['button_text'] : 'Call API';
95
96 ob_start(); ?>
97
98 <button
99 class="button btn btn-primary wpgetapi_ajax_request"
100 data-api="<?php esc_html_e( $a['api_id'] ); ?>"
101 data-endpoint="<?php esc_html_e( $a['endpoint_id'] ); ?>"
102 data-args="<?php esc_html_e( json_encode( $a['args'] ) ); ?>"
103 data-keys="<?php esc_html_e( json_encode( $a['keys'] ) ); ?>">
104 <span class="button_text"><?php esc_html_e( $button_text ); ?></span>
105 </button>
106 <div class="wpgetapi_ajax_output"></div>
107
108 <?php
109 $result = ob_get_contents();
110 ob_end_clean();
111
112 } else {
113
114 $result = 'You need the <a href="https://wpgetapi.com/downloads/pro-plugin">PRO plugin</a> for this AJAX functionality.';
115
116 }
117
118 } else {
119
120 // [wpgetapi_endpoint api_id='livelearn' endpoint_id='dezzp1' chain='start' keys='UUID']
121 // [wpgetapi_endpoint api_id='livelearn' endpoint_id='dezzp2' chain='end']
122
123 // if we are starting the chain
124 if( isset( $a['args']['chain'] ) ) {
125
126 // get our result from this chain
127 $result = wpgetapi_endpoint( $a['api_id'], $a['endpoint_id'], $a['args'], $a['keys'] );
128
129 // set the transient for this chain if not the last chain
130 if( $a['args']['chain'] !== 'end' )
131 set_transient( 'wpgetapi_chain_' . sanitize_text_field( $a['args']['chain'] ), $result, 10 );
132
133
134 } else {
135
136 // do our standard return
137 $result = wpgetapi_endpoint( $a['api_id'], $a['endpoint_id'], $a['args'], $a['keys'] );
138
139 }
140
141
142
143
144 }
145
146 if( is_array( $result ) ) {
147 $result = sprintf(
148 __( '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' ),
149 '<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>'
150 );
151 }
152
153 if( $a['args']['format'] !== 'no_display' )
154 return $result;
155
156 }
157 add_shortcode( 'wpgetapi_endpoint', 'wpgetapi_endpoint_shortcode' );
158