Add an "Export Pods Table" action to the TablePress menu (#6)
Resolves #1. Resolves #5. Co-authored-by: Glen Whitney <glen@studioinfinity.org> Reviewed-on: #6 Co-Authored-By: Glen Whitney <glen@nobody@nowhere.net> Co-Committed-By: Glen Whitney <glen@nobody@nowhere.net>
This commit is contained in:
parent
c18048cb1a
commit
6811af5970
4 changed files with 295 additions and 10 deletions
|
@ -3,9 +3,9 @@
|
|||
Plugin Name: TablePress Extension: Pods tables
|
||||
Plugin URI: https://code.studioinfinity.org/glen/tablepress-pods
|
||||
Description: Custom Extension for TablePress to incorporate Pods information in tables
|
||||
Version: 0.2
|
||||
Version: 0.3.0
|
||||
Author: Glen Whitney
|
||||
Author URI: http ://studioinfinity.org/
|
||||
Author URI: http://studioinfinity.org/
|
||||
*/
|
||||
|
||||
/* Copyright (C) 2018-2019 Glen Whitney
|
||||
|
@ -58,16 +58,58 @@ Street, Fifth Floor, Boston, MA 02110-1301 , USA.
|
|||
add_filter( 'tablepress_shortcode_table_default_shortcode_atts',
|
||||
'playground_add_shortcode_parameter_pods' );
|
||||
add_filter( 'tablepress_table_raw_render_data', 'playground_expand_pod', 10, 2 );
|
||||
add_filter( 'tablepress_admin_view_actions', 'playground_add_export_pod_action');
|
||||
add_filter( 'tablepress_load_file_full_path', 'playground_exportpods_path', 10, 3);
|
||||
add_filter( 'tablepress_view_data', 'playground_exportpods_data', 10, 2);
|
||||
add_action( 'admin_post_tablepress_exportpods', 'playground_handle_exportpods');
|
||||
|
||||
function playground_add_shortcode_parameter_pods( $default_atts ) {
|
||||
$default_atts['pod_name'] = '';
|
||||
$default_atts['pod_name'] = '';
|
||||
$default_atts['pod_dump'] = '';
|
||||
$default_atts['pod_where'] = '';
|
||||
return $default_atts;
|
||||
return $default_atts;
|
||||
}
|
||||
|
||||
function playground_add_export_pod_action( $view_actions ) {
|
||||
$view_actions['exportpods'] = array(
|
||||
'show_entry' => true,
|
||||
'page_title' => __( 'Export Pods Table', 'tablepress' ),
|
||||
'admin_menu_title' => __( 'Export Pods Table', 'tablepress' ),
|
||||
'nav_tab_title' => _x( 'Export Pods', 'navigation bar', 'tablepress' ),
|
||||
'required_cap' => 'tablepress_export_tables',
|
||||
);
|
||||
return $view_actions;
|
||||
}
|
||||
|
||||
function playground_exportpods_path( $fullpath, $filename, $directory ) {
|
||||
if ($filename !== 'view-exportpods.php') return $fullpath;
|
||||
return plugin_dir_path( __FILE__ ) . $filename;
|
||||
}
|
||||
|
||||
function playground_exportpods_data( $data, $act ) {
|
||||
if ($act !== 'exportpods') return $data;
|
||||
// Load all table IDs without priming the post meta cache, as table options/visibility are not needed.
|
||||
$data['table_ids'] = TablePress::$model_table->load_all( false );
|
||||
$data['tables_count'] = TablePress::$model_table->count_tables();
|
||||
if ( ! empty( $_GET['table_id'] ) ) {
|
||||
$data['export_ids'] = explode( ',', $_GET['table_id'] );
|
||||
} else {
|
||||
// Just show empty export form.
|
||||
$data['export_ids'] = array();
|
||||
}
|
||||
$exporter = TablePress::load_class( 'TablePress_Export', 'class-export.php', 'classes' );
|
||||
$data['zip_support_available'] = false;
|
||||
$data['export_formats'] = $exporter->export_formats;
|
||||
$data['export_format'] = ( ! empty( $_GET['export_format'] ) ) ? $_GET['export_format'] : false;
|
||||
$data['csv_delimiters'] = $exporter->csv_delimiters;
|
||||
$data['csv_delimiter'] = ( ! empty( $_GET['csv_delimiter'] ) ) ? $_GET['csv_delimiter'] : _x( ',', 'Default CSV delimiter in the translated language (";", ",", or "tab")', 'tablepress' );
|
||||
$data['pods'] = pods_api()->load_pods();
|
||||
$data['pod_name'] = ( ! empty( $_GET['pod_name'] ) ) ? $_GET['pod_name'] : false;
|
||||
return $data;
|
||||
}
|
||||
|
||||
function playground_expand_pod( $table, $render_options) {
|
||||
if (empty($render_options['pod_name'])) return $table;
|
||||
if (empty($render_options['pod_name'])) return $table;
|
||||
$pod_params = array('limit'=>-1);
|
||||
if (!empty($render_options['pod_where'])) {
|
||||
$pod_params['where'] = $render_options['pod_where'];
|
||||
|
@ -128,7 +170,7 @@ function playground_expand_pod( $table, $render_options) {
|
|||
while ($pod->fetch()) {
|
||||
$nrow = array();
|
||||
$nrow[] = $pod->field('id');
|
||||
$nrow[] = $pod->field('post_date');
|
||||
$nrow[] = $pod->display('post_date');
|
||||
$nrow[] = $pod->field('title');
|
||||
$nrow[] = get_the_author_meta('display_name', $pod->field('author'));
|
||||
$nrow[] = $pod->field('permalink');
|
||||
|
@ -145,3 +187,37 @@ function playground_expand_pod( $table, $render_options) {
|
|||
$table['visibility']['columns'] = $cvis;
|
||||
return $table;
|
||||
}
|
||||
|
||||
function playground_handle_exportpods () {
|
||||
TablePress::check_nonce( 'exportpods' );
|
||||
|
||||
if ( ! current_user_can( 'tablepress_export_tables' ) ) {
|
||||
wp_die( __( 'Sorry, you are not allowed to access this page.', 'default' ), 403 );
|
||||
}
|
||||
if ( empty( $_POST['export'] ) || ! is_array( $_POST['export'] ) ) {
|
||||
TablePress::redirect( array( 'action' => 'export', 'message' => 'error_export' ) );
|
||||
} else {
|
||||
$export = wp_unslash( $_POST['export'] );
|
||||
}
|
||||
|
||||
/* Reset the nonce for forwarding the action */
|
||||
$_REQUEST['_wpnonce'] = wp_create_nonce( TablePress::nonce( 'export' ) );
|
||||
|
||||
/* Set up the filter using the pod params */
|
||||
$r_opts = array('pod_name' => $export['pod_name'],
|
||||
'pod_where' => $export['pod_where']
|
||||
);
|
||||
if ( isset( $export['pod_dump'] ) ) {
|
||||
$r_opts['pod_dump'] = true;
|
||||
}
|
||||
$filter = function( $data, $tab, $fmt, $delim ) use ($r_opts) {
|
||||
$newtab = playground_expand_pod($tab, $r_opts);
|
||||
$exporter = TablePress::load_class( 'TablePress_Export', 'class-export.php', 'classes' );
|
||||
return $exporter->export_table( $newtab, $fmt, $delim );
|
||||
};
|
||||
|
||||
/* Temporarily set a filter on exporting */
|
||||
add_filter( 'tablepress_export_data', $filter, 10, 4);
|
||||
do_action( 'admin_post_tablepress_export' );
|
||||
remove_filter( 'tablepress_export_data', $filter, 10 );
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue