| 1 |
<?php |
| 2 |
/** |
| 3 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 4 |
* |
| 5 |
* @package WPDataAccess\Utilities |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPDataAccess\Utilities { |
| 9 |
|
| 10 |
/** |
| 11 |
* Class WPDA_Example |
| 12 |
* |
| 13 |
* Show downloadable example code. |
| 14 |
* |
| 15 |
* @package WPDataAccess\Utilities |
| 16 |
* @author Peter Schulz |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
class WPDA_Example { |
| 20 |
|
| 21 |
/** |
| 22 |
* Send example file |
| 23 |
* |
| 24 |
* @since 1.0.0 |
| 25 |
*/ |
| 26 |
public function get_example() { |
| 27 |
|
| 28 |
if ( isset( $_REQUEST['filename'] ) ) { |
| 29 |
$filename = sanitize_text_field( wp_unslash( $_REQUEST['filename'] ) ); // input var okay. |
| 30 |
if ( 'wpda-test' === $filename ) { |
| 31 |
$this->send_file( $filename ); |
| 32 |
} else { |
| 33 |
wp_die( esc_html__( 'ERROR: Wrong arguments', 'wp-data-access' ) ); |
| 34 |
} |
| 35 |
} else { |
| 36 |
wp_die( esc_html__( 'ERROR: Wrong arguments', 'wp-data-access' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Send content of example file |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* |
| 46 |
* @param string $example_name Name of example file without file extention. |
| 47 |
*/ |
| 48 |
protected function send_file( $example_name ) { |
| 49 |
|
| 50 |
$example_file_name = plugin_dir_path( dirname( __FILE__ ) ) . '../tutorials/' . $example_name . '.php.txt'; |
| 51 |
$example_file_handle = fopen( $example_file_name, 'r' ); |
| 52 |
if ( $example_file_handle ) { |
| 53 |
// Read file content and close handle. |
| 54 |
$example_file_content = fread( $example_file_handle, filesize( $example_file_name ) ); |
| 55 |
fclose( $example_file_handle ); |
| 56 |
|
| 57 |
// Send file content. |
| 58 |
header( 'Content-type: text/plain; charset=utf-8' ); |
| 59 |
header( "Content-Disposition: attachment; filename=$example_name.php.txt" ); |
| 60 |
header( 'Pragma: no-cache' ); |
| 61 |
header( 'Expires: 0' ); |
| 62 |
echo $example_file_content; |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
|