| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die('Naw ya dinnie!'); |
| 4 |
|
| 5 |
/** |
| 6 |
* Stream the CSV export before any admin page HTML is output. Hooked on admin_init |
| 7 |
* rather than handled in the page callback itself, since by the time WordPress calls |
| 8 |
* the page callback it has already sent the admin header/menu chrome, making it too |
| 9 |
* late to send Content-Disposition/Content-Type headers for a file download. |
| 10 |
*/ |
| 11 |
function sh_cd_admin_page_import_maybe_export() { |
| 12 |
|
| 13 |
if ( false === isset( $_GET[ 'page' ] ) || 'sh-cd-import' !== $_GET[ 'page' ] || 'export' !== ( $_GET[ 'mode' ] ?? '' ) ) { |
| 14 |
return; |
| 15 |
} |
| 16 |
|
| 17 |
sh_cd_permission_check(); |
| 18 |
|
| 19 |
if ( false === sh_cd_is_premium() ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
check_admin_referer( 'sh_cd_export_csv' ); |
| 24 |
|
| 25 |
$filename = 'snippet-shortcodes-export-' . gmdate( 'Y-m-d' ) . '.csv'; |
| 26 |
|
| 27 |
nocache_headers(); |
| 28 |
header( 'Content-Type: text/csv; charset=utf-8' ); |
| 29 |
header( 'Content-Disposition: attachment; filename=' . $filename ); |
| 30 |
|
| 31 |
echo sh_cd_export_csv(); |
| 32 |
exit; |
| 33 |
} |
| 34 |
add_action( 'admin_init', 'sh_cd_admin_page_import_maybe_export' ); |
| 35 |
|
| 36 |
function sh_cd_admin_page_import() { |
| 37 |
|
| 38 |
sh_cd_permission_check(); |
| 39 |
|
| 40 |
wp_enqueue_media(); |
| 41 |
|
| 42 |
$importing = false; |
| 43 |
$output = ''; |
| 44 |
|
| 45 |
if ( true === sh_cd_is_premium() && |
| 46 |
false === empty( $_POST[ 'attachment-id' ] ) ){ |
| 47 |
|
| 48 |
$importing = true; |
| 49 |
$dry_run = ( false === empty( $_POST[ 'dry-run' ] ) ); |
| 50 |
$output = sh_cd_import_csv( $_POST[ 'attachment-id' ], $dry_run ); |
| 51 |
} |
| 52 |
|
| 53 |
?> |
| 54 |
<div class="wrap sh-cd-csv-import ws-ls-admin-page"> |
| 55 |
<div id="poststuff"> |
| 56 |
<div id="post-body" class="metabox-holder"> |
| 57 |
<div id="post-body-content"> |
| 58 |
<div class="meta-box-sortables ui-sortable"> |
| 59 |
<?php |
| 60 |
if ( false === sh_cd_is_premium() ) { |
| 61 |
sh_cd_display_pro_upgrade_notice(); |
| 62 |
} |
| 63 |
?> |
| 64 |
<div class="postbox"> |
| 65 |
<h3 class="postbox-header"> |
| 66 |
<span> |
| 67 |
<?php echo __( 'Import CSV', SH_CD_SLUG ); ?> |
| 68 |
</span> |
| 69 |
</h3> |
| 70 |
<div class="inside"> |
| 71 |
<?php if ( false === $importing ): ?> |
| 72 |
<div class="sh-cd-form-row"> |
| 73 |
<p> |
| 74 |
<?php echo __( 'Please select a CSV file to import one or more shortcodes into your collection.', SH_CD_SLUG ); ?> |
| 75 |
<a href="https://yeken.gitbook.io/snippet-shortcodes/features/csv-import" rel="noopener noreferrer" target="_blank"><?php echo __( 'Read more about CSV imports and the required format', SH_CD_SLUG ); ?>.</a> |
| 76 |
</p> |
| 77 |
<input id="select_csv" type="button" class="button sh-cd-button" value="<?php echo __( 'Select CSV file', SH_CD_SLUG ); ?>" /> |
| 78 |
<br /> |
| 79 |
</div> |
| 80 |
<div class="sh-cd-hide sh-cd-import-selected" id="selected-form" > |
| 81 |
<form action="<?php echo admin_url( 'admin.php?page=sh-cd-import&mode=import'); ?>" method="post"> |
| 82 |
<div class="sh-cd-form-row"> |
| 83 |
<label for="attachment-path"><?php echo __( 'Selected file:', SH_CD_SLUG ); ?></label> |
| 84 |
<input type='text' name='attachment-path' id='attachment-path' value='' class="widefat" disabled="disabled" /> |
| 85 |
<input type='hidden' name='attachment-id' id='attachment-id' value='' /> |
| 86 |
</div> |
| 87 |
<div class="sh-cd-form-row"> |
| 88 |
<input type='checkbox' name='dry-run' id='dry-run' value='yes' /> |
| 89 |
<label for="dry-run"><?php echo __( 'Dry run mode. This will do basic tests on the file without performing an import.', SH_CD_SLUG ); ?></label> |
| 90 |
</div> |
| 91 |
<div class="sh-cd-form-row"> |
| 92 |
<input type="submit" class="button button-primary sh-cd-button" value="<?php echo __( 'Import CSV', SH_CD_SLUG ); ?>" <?php if ( false === sh_cd_is_premium() ) { echo 'disabled="disabled"'; } ?> /> |
| 93 |
</div> |
| 94 |
</form> |
| 95 |
</div> |
| 96 |
<?php else: ?> |
| 97 |
<p><strong><?php echo __( 'Output:', SH_CD_SLUG ); ?></strong></p> |
| 98 |
<textarea class="widefat" rows="20" cols="100"><?php echo esc_html( $output ); ?></textarea> |
| 99 |
<?php endif; ?> |
| 100 |
</div> |
| 101 |
</div> |
| 102 |
<div class="postbox"> |
| 103 |
<h3 class="postbox-header"> |
| 104 |
<span> |
| 105 |
<?php echo __( 'Export to CSV', SH_CD_SLUG ); ?> |
| 106 |
</span> |
| 107 |
</h3> |
| 108 |
<div class="inside"> |
| 109 |
<div class="sh-cd-form-row"> |
| 110 |
<p><?php echo __( 'Download all of your shortcodes as a CSV file, in the same format used for import.', SH_CD_SLUG ); ?></p> |
| 111 |
<a class="button button-primary sh-cd-button" <?php if ( false === sh_cd_is_premium() ) { echo 'disabled="disabled"'; } ?> href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=sh-cd-import&mode=export' ), 'sh_cd_export_csv' ) ); ?>"><?php echo __( 'Export CSV', SH_CD_SLUG ); ?></a> |
| 112 |
</div> |
| 113 |
</div> |
| 114 |
</div> |
| 115 |
</div> |
| 116 |
</div> |
| 117 |
</div> |
| 118 |
<br class="clear"> |
| 119 |
</div> |
| 120 |
<script> |
| 121 |
jQuery( document ).ready(function ($) { |
| 122 |
|
| 123 |
// CSV import for |
| 124 |
let file_frame; |
| 125 |
|
| 126 |
$( '#select_csv').on('click', function( event ){ |
| 127 |
|
| 128 |
event.preventDefault(); |
| 129 |
|
| 130 |
<?php if ( false === sh_cd_is_premium() ) : ?> |
| 131 |
alert( '<?php echo __( "Please upgrade to bulk import shortcodes via CSV.", SH_CD_SLUG ); ?>' ); |
| 132 |
return; |
| 133 |
<?php else: ?> |
| 134 |
|
| 135 |
// If the media frame already exists, reopen it. |
| 136 |
if ( file_frame ) { |
| 137 |
|
| 138 |
// Open frame |
| 139 |
file_frame.open(); |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
// Create the media frame. |
| 144 |
file_frame = wp.media.frames.file_frame = wp.media({ |
| 145 |
title: '<?php echo __( "Select a CSV", SH_CD_SLUG ); ?>', |
| 146 |
button: { |
| 147 |
text: '<?php echo __( "Use this file", SH_CD_SLUG ); ?>', |
| 148 |
}, |
| 149 |
library : { |
| 150 |
type : ['application/csv', 'text/csv'], |
| 151 |
}, |
| 152 |
multiple: false |
| 153 |
}); |
| 154 |
|
| 155 |
// When an image is selected, run a callback. |
| 156 |
file_frame.on( 'select', function() { |
| 157 |
|
| 158 |
attachment = file_frame.state().get('selection').first().toJSON(); |
| 159 |
|
| 160 |
$( '#attachment-id' ).val( attachment.id ); |
| 161 |
$( '#attachment-path' ).val( attachment.url ); |
| 162 |
$( '#selected-form' ).removeClass( 'sh-cd-hide' ); |
| 163 |
|
| 164 |
}); |
| 165 |
|
| 166 |
file_frame.open(); |
| 167 |
|
| 168 |
<?php endif; ?> |
| 169 |
}); |
| 170 |
}); |
| 171 |
</script> |
| 172 |
<?php |
| 173 |
} |
| 174 |
|