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