diff --git a/README.md b/README.md index ad3706e..349a1c8 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,15 @@ This project also assumes that you have installed the Pods plug-in (see https:// The tablepress-pods extension allows you to extract and display in Tablepress tables information from the content in Pods custom post types and taxonomies. If you use Pods and would like to produce tables from the information you've stored in them, this extension is potentially very useful. -##### Installation +##### Installation/Upgrade -Download a release from https://code.studioinfinity.org/glen/tablepress-pods/releases (as a zip file) and install and activate in your WordPress site like any other extension in zip format. (Namely, from your Dashboard, select Plugins, click on add new, and then browse to the downloaded zip file on your disk.) +Download a release from https://code.studioinfinity.org/glen/tablepress-pods/releases (as a zip file) and install and activate in your WordPress site like any other extension in zip format. Namely, from your Dashboard, you can: + +1. Select Plugins from the left-hand menu bar. +1. Click on "Add New" and then "Upload Plugin." +1. Browse to the downloaded zip file on your local disk, and click "Install Now." +1. If this is an upgrade, it will show you the currently installed version and the new version and ask you to + confirm the replacement. ##### Usage @@ -38,4 +44,7 @@ Note that the {}-expressions allowed in the entries of the table definition incl ##### Building -To produce an installable zip file, change directories into a git clone of this repository, and simply execute the command in mkplugin.sh (for example, via `bash mkplugin.sh`). Note that currently you must update the version number in the mkplugin.sh command. \ No newline at end of file +To produce an installable zip file, assuming you have cloned this repository: +1. Make sure that you have committed your changes with an update to the Version parameter in tablepress-pods.php. +1. Make sure the current git head is on the commit you want to build. +1. From the top-level directory of the repository, execute `bash mkplugin.sh`. diff --git a/mkplugin.sh b/mkplugin.sh index a530d7f..ab4f6e7 100644 --- a/mkplugin.sh +++ b/mkplugin.sh @@ -1 +1,5 @@ -git archive HEAD --prefix=tablepress-pods/ --format=zip -o tablepress-pods-0.2.zip +verline=$(grep Version tablepress-pods.php) +cvline=${verline//[$'\t\r\n']} +destfile="tablepress-pods-${cvline#V*: }.zip" +echo "Writing $destfile..." +git archive HEAD --prefix=tablepress-pods/ --format=zip -o $destfile diff --git a/tablepress-pods.php b/tablepress-pods.php index a96541a..e49f526 100644 --- a/tablepress-pods.php +++ b/tablepress-pods.php @@ -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 ); +} diff --git a/view-exportpods.php b/view-exportpods.php new file mode 100644 index 0000000..a660a76 --- /dev/null +++ b/view-exportpods.php @@ -0,0 +1,196 @@ +process_action_messages( array( + 'error_export' => __( 'Error: The export failed.', 'tablepress' ), + 'error_load_table' => __( 'Error: This table could not be loaded!', 'tablepress' ), + 'error_table_corrupted' => __( 'Error: The internal data of this table is corrupted!', 'tablepress' ), + ) ); + + $this->add_text_box( 'head', array( $this, 'textbox_head' ), 'normal' ); + if ( 0 === $data['tables_count'] ) { + $this->add_meta_box( 'no-tables', __( 'Export Tables', 'tablepress' ), array( $this, 'postbox_no_tables' ), 'normal' ); + } else { + $this->admin_page->enqueue_script( 'export', array( 'jquery' ) ); + $this->add_meta_box( 'export-form', __( 'Export Pods Tables', 'tablepress' ), array( $this, 'postbox_export_form' ), 'normal' ); + $this->data['submit_button_caption'] = _x( 'Download Pods Export File', 'button', 'tablepress' ); + $this->add_text_box( 'submit', array( $this, 'textbox_submit_button' ), 'submit' ); + } + } + + /** + * Print the screen head text. + * + * @since 0.2.6 + * + * @param array $data Data for this screen. + * @param array $box Information about the text box. + */ + public function textbox_head( array $data, array $box ) { + ?> +

+ + +

+

+ +
+ + +

+ 'add' ) ); + $import_url = TablePress::url( array( 'action' => 'import' ) ); + ?> +

+

add or import a table to get started!', 'tablepress' ), $add_url, $import_url ); ?>

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +
+ +
+ +
+ +
+ + + + +
+ + + + +
+