| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Pretty print when debugging is enabled in options |
| 5 |
*/ |
| 6 |
if ( ! function_exists( 'wpgetapi_pp' ) ) { |
| 7 |
function wpgetapi_pp( $array_data ) { |
| 8 |
echo '<pre style="white-space:pre-wrap;">'; |
| 9 |
print_r( $array_data ); |
| 10 |
echo '</pre>' . "\n"; |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
/** |
| 16 |
* Dropdown options for results_format |
| 17 |
* It is set up this way so we can easily add the filter to be able to add extra format options |
| 18 |
* |
| 19 |
*/ |
| 20 |
function wpgetapi_results_format_options( $field ) { |
| 21 |
$options = apply_filters( |
| 22 |
'wpgetapi_results_format_options', |
| 23 |
array( |
| 24 |
'json_string' => __( 'JSON string', 'wpgetapi' ), |
| 25 |
'json_decoded' => __( 'PHP array data', 'wpgetapi' ), |
| 26 |
) |
| 27 |
); |
| 28 |
return $options; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Recursive sanitation for text or array |
| 34 |
* |
| 35 |
* @param $array_or_string (array|string) |
| 36 |
* @since 1.0.0 |
| 37 |
* @return mixed |
| 38 |
*/ |
| 39 |
function wpgetapi_sanitize_text_or_array( $array_or_string ) { |
| 40 |
if ( is_string( $array_or_string ) ) { |
| 41 |
$array_or_string = sanitize_text_field( $array_or_string ); |
| 42 |
} elseif ( is_array( $array_or_string ) ) { |
| 43 |
foreach ( $array_or_string as $key => &$value ) { |
| 44 |
if ( is_array( $value ) ) { |
| 45 |
$value = wpgetapi_sanitize_text_or_array( $value ); |
| 46 |
} else { |
| 47 |
$value = sanitize_text_field( $value ); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return $array_or_string; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* wpgetapi_output_endpoint_test_area |
| 58 |
* |
| 59 |
*/ |
| 60 |
function wpgetapi_output_top_of_endpoint( $field_args, $field ) { |
| 61 |
|
| 62 |
ob_start(); |
| 63 |
|
| 64 |
$test_disabled = true; |
| 65 |
|
| 66 |
if ( $field->value ) { |
| 67 |
$test_disabled = false; |
| 68 |
} |
| 69 |
|
| 70 |
if ( is_ssl() ) { |
| 71 |
$copy_template_tag_html = '<span title="' . esc_html__( 'Copy template tag', 'wpgetapi' ) . '" class="copy-this dashicons dashicons-admin-page"></span>'; |
| 72 |
$copy_shortcode_html = '<span title="' . esc_html__( 'Copy shortcode', 'wpgetapi' ) . '" class="copy-this dashicons dashicons-admin-page"></span>'; |
| 73 |
} else { |
| 74 |
$copy_template_tag_html = ''; |
| 75 |
$copy_shortcode_html = ''; |
| 76 |
} |
| 77 |
?> |
| 78 |
|
| 79 |
<pre class="functions"> |
| 80 |
<div class="wrapper"><div>Template Tag:</div> <span class="wpgetapi-template-tag wpgetapi-copy">wpgetapi_endpoint( '<?php echo esc_html( $field->args['api_id'] ); ?>', '<span class='endpoint_id'></span>', array( 'debug' => false ) );</span> <?php echo $copy_template_tag_html; ?></div> |
| 81 |
<div class="wrapper"><div>Shortcode:</div> <span class="wpgetapi-shortcode wpgetapi-copy">[wpgetapi_endpoint api_id='<?php echo esc_html( $field->args['api_id'] ); ?>' endpoint_id='<span class='endpoint_id'></span>' debug='false']</span> <?php echo $copy_shortcode_html; ?></div> |
| 82 |
</pre> |
| 83 |
|
| 84 |
<div class="wpgetapi-test-area" data-endpoint="<?php echo esc_attr( $field->value ); ?>" data-api="<?php echo esc_attr( $field->args['api_id'] ); ?>"> |
| 85 |
|
| 86 |
<span class="handle" style="display:none">Hide/Show Results</span> |
| 87 |
|
| 88 |
<?php do_action( 'wpgetapi_admin_before_test_result' ); ?> |
| 89 |
|
| 90 |
<div class="wpgetapi-result"></div> |
| 91 |
</div> |
| 92 |
|
| 93 |
<div class="fields-wrapper"> |
| 94 |
|
| 95 |
<?php |
| 96 |
|
| 97 |
$content = ob_get_contents(); |
| 98 |
ob_end_clean(); |
| 99 |
return $content; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Ensure our unique ID's are only lowercase and underscores |
| 105 |
* |
| 106 |
* @param mixed $value The unsanitized value from the form. |
| 107 |
* @param array $field_args Array of field arguments. |
| 108 |
* @param CMB2_Field $field The field object |
| 109 |
* |
| 110 |
* @return mixed Sanitized value to be stored. |
| 111 |
*/ |
| 112 |
function wpgetapi_sanitize_unique_id( $value, $field_args, $field ) { |
| 113 |
$sanitized_value = sanitize_file_name( $value ); |
| 114 |
$sanitized_value = str_replace( '-', '_', $sanitized_value ); |
| 115 |
$sanitized_value = str_replace( '.', '_', $sanitized_value ); |
| 116 |
$sanitized_value = str_replace( '^', '_', $sanitized_value ); |
| 117 |
$sanitized_value = str_replace( '@', '_', $sanitized_value ); |
| 118 |
return $sanitized_value; |
| 119 |
} |
| 120 |
|
| 121 |
|