PluginProbe
WPGet API – Connect to any external REST API / 2.0.4
WPGet API – Connect to any external REST API v2.0.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 / frontend / functions.php

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

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