feat: Allow shortcodes in attribute values (#11)

Since wordpress also doesn't allow brackets in attribute values, first
replace `((` and `))` by `[` and `]` before expanding the shortcodes.

Expands the README to full documentation, and renames internal functions
to make more sense.

Resolves #10.

Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Reviewed-on: #11
This commit is contained in:
Glen Whitney 2022-03-28 23:08:13 +00:00
parent 29daaf7da0
commit 2e4d61b4ac
2 changed files with 64 additions and 19 deletions

View file

@ -3,7 +3,7 @@
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.3.0
Version: 0.4.1
Author: Glen Whitney
Author URI: http://studioinfinity.org/
*/
@ -44,6 +44,11 @@ Street, Fifth Floor, Boston, MA 02110-1301 , USA.
* designation, even though it contains magic tags; hence, the
* magic tags will appear in the result.
*
* Note that in the pod_name and pod_where parameters, `((` and `))` are
* replaced by `[` and `]` respectively and do_shortcode() is run on the
* result, to facilitate the use of shortcodes to specify the values of these
* parameters.
*
* [table id=1 pod_name="mypod" pod_dump=true pod_where="t.myfield = 3" /]
*
* *Ignores* the contents of the table altogether, and replaces it with a
@ -56,21 +61,21 @@ 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');
'tbp_pods_add_shortcode_parameter_pods' );
add_filter( 'tablepress_table_raw_render_data', 'tbp_pods_expand_pod', 10, 2 );
add_filter( 'tablepress_admin_view_actions', 'tbp_pods_add_export_pod_action');
add_filter( 'tablepress_load_file_full_path', 'tbp_pods_exportpods_path', 10, 3);
add_filter( 'tablepress_view_data', 'tbp_pods_exportpods_data', 10, 2);
add_action( 'admin_post_tablepress_exportpods', 'tbp_pods_handle_exportpods');
function playground_add_shortcode_parameter_pods( $default_atts ) {
function tbp_pods_add_shortcode_parameter_pods( $default_atts ) {
$default_atts['pod_name'] = '';
$default_atts['pod_dump'] = '';
$default_atts['pod_where'] = '';
return $default_atts;
}
function playground_add_export_pod_action( $view_actions ) {
function tbp_pods_add_export_pod_action( $view_actions ) {
$view_actions['exportpods'] = array(
'show_entry' => true,
'page_title' => __( 'Export Pods Table', 'tablepress' ),
@ -81,12 +86,12 @@ function playground_add_export_pod_action( $view_actions ) {
return $view_actions;
}
function playground_exportpods_path( $fullpath, $filename, $directory ) {
function tbp_pods_exportpods_path( $fullpath, $filename, $directory ) {
if ($filename !== 'view-exportpods.php') return $fullpath;
return plugin_dir_path( __FILE__ ) . $filename;
}
function playground_exportpods_data( $data, $act ) {
function tbp_pods_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 );
@ -108,14 +113,21 @@ function playground_exportpods_data( $data, $act ) {
return $data;
}
function playground_expand_pod( $table, $render_options) {
function tbp_pods_maybe_shortcodes($value) {
$bracketed = str_replace(array('((', '))'), array('[', ']'), $value);
return do_shortcode($bracketed);
}
function tbp_pods_expand_pod( $table, $render_options) {
if (empty($render_options['pod_name'])) return $table;
$final_pod_name = tbp_pods_maybe_shortcodes($render_options['pod_name']);
$pod_params = array('limit'=>-1);
if (!empty($render_options['pod_where'])) {
$pod_params['where'] = $render_options['pod_where'];
$pod_params['where'] =
tbp_pods_maybe_shortcodes($render_options['pod_where']);
}
$pod = pods($render_options['pod_name'], $pod_params);
$ndata = array();
$pod = pods($final_pod_name, $pod_params);
$ndata = array();
$rvis = array();
$cvis = array();
if (empty($render_options['pod_dump'])) {
@ -188,7 +200,7 @@ function playground_expand_pod( $table, $render_options) {
return $table;
}
function playground_handle_exportpods () {
function tbp_pods_handle_exportpods () {
TablePress::check_nonce( 'exportpods' );
if ( ! current_user_can( 'tablepress_export_tables' ) ) {
@ -211,7 +223,7 @@ function playground_handle_exportpods () {
$r_opts['pod_dump'] = true;
}
$filter = function( $data, $tab, $fmt, $delim ) use ($r_opts) {
$newtab = playground_expand_pod($tab, $r_opts);
$newtab = tbp_pods_expand_pod($tab, $r_opts);
$exporter = TablePress::load_class( 'TablePress_Export', 'class-export.php', 'classes' );
return $exporter->export_table( $newtab, $fmt, $delim );
};