Add the code of the plugin

This commit is contained in:
Glen Whitney 2019-09-14 11:42:34 -04:00
parent 0e380c6c68
commit c4c2fc9e3d
5 changed files with 192 additions and 2 deletions

4
.gitattributes vendored Normal file
View File

@ -0,0 +1,4 @@
.git* export-ignore
README* export-ignore
LICENSE* export-ignore
mk* export-ignore

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*~
*.zip

View File

@ -1,3 +1,39 @@
# tablepress-pods
## tablepress-pods
Create Tablepress tables which summarize and display information from Pods
##### Abstract
Create Tablepress tables which summarize and display information from Pods
##### Description
This project is an extension to the TablePress plug-in (see https://tablepress.org/) for WordPress (https://wordpress.org/). It is not useful without the TablePress plug-in. This project is not part of TablePress, but designed to work with it and extend its capabilities.
This project also assumes that you have installed the Pods plug-in (see https://pods.io/) in your WordPress installation. It is also not useful without the Pods plug-in.
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
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.)
##### Usage
This extension operates by adding additional parameters to the [table id=NN /] shortcode. For a list of the provided parameters and their meanings, see the source file tablepress-pods.php. For it to be useful, you then need to go to the
table definition (in the Tablepress tabs from the Dashboard) of the corresponding table and use Pods "magic tags" within that table.
Here's a brief example:
`[table id=2 pod_name="problem" /]`
together with a definition of table 2 in Tablepress that looks like
| | A | B |
| ----: | :---------- | ------------------------------------- |
| **1** | Number | Title |
| **2** | `{@number}` | `<a href="{@permalink}">{@title}</a>` |
will produce a two-column table that lists the number and title of each "problem" in the Pod, with the title being a link to the problem.
##### 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.

1
mkplugin.sh Normal file
View File

@ -0,0 +1 @@
git archive HEAD --prefix=tablepress-pods/ --format=zip -o tablepress-pods-0.2.zip

147
tablepress-pods.php Normal file
View File

@ -0,0 +1,147 @@
<?php
/*
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
Author: Glen Whitney
Author URI: http ://studioinfinity.org/
*/
/* Copyright (C) 2018-2019 Glen Whitney
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301 , USA.
*/
/*
* Usage and possible parameters:
* [table id=1 pod_name="mypod" pod_where="t.myfield = 'important'" /]
*
* Each row of the table which contains a Pods "magic tag" anywhere is expanded
* into one row for each of the pod records returned by
* pods(<pod_name>)->find(<pod_where>), with all of the magic tags in the row
* expanded for the corresponding record.
*
* The expansion can be controlled by the following "pseudo-shortcodes" which
* must occur at the beginning of the first column of the row (you may want to
* dedicate a hidden column just for this purpose if you are making use of these)
* [once] -- this row expands only into a single row (corresponding to the
* first record returned by the find() call), with magic
* tags expanded
* [literal] -- do not expand this row, except for removing the [literal]
* designation, even though it contains magic tags; hence, the
* magic tags will appear in the result.
*
* [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
* simple dump of the ID, date, title, author, permalink, and all custom
* Pods fields for each record in a Pods custom post type. Note that the
* taxonomies, if any, of the post type will not be shown. Useful mainly for
* seeing what is present in your Pod; for any presentation tables you will
* most likely want to use the first form.
*
*/
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 );
function playground_add_shortcode_parameter_pods( $default_atts ) {
$default_atts['pod_name'] = '';
$default_atts['pod_dump'] = '';
$default_atts['pod_where'] = '';
return $default_atts;
}
function playground_expand_pod( $table, $render_options) {
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'];
}
$pod = pods($render_options['pod_name'], $pod_params);
$ndata = array();
$rvis = array();
$cvis = array();
if (empty($render_options['pod_dump'])) {
# Standard case: expand rows with magic tags into multiple rows,
# one for each record in $pod, controlled by the pseudo-shortcodes
$cvis = $table['visibility']['columns'];
$orig_rvis = $table['visibility']['rows'];
foreach ($table['data'] as $row_idx => $row) {
# First, check for the [literal] code since nothing much to do then
if (0 === strpos($row[0], '[literal]')) {
$row[0] = substr($row[0], 9);
$ndata[] = $row;
$rvis[] = $orig_rvis[$row_idx];
continue;
}
# Similarly, if no magic tags, just copy the row.
$wholerow = implode('',$row);
if (!preg_match('/{@.*}/', $wholerow)) {
$ndata[] = $row;
$rvis[] = $orig_rvis[$row_idx];
continue;
}
# OK, we need to expand magic tags in the row. First check for
# the [once] pseudo-shortcode:
$one_timer = FALSE;
if (0 === strpos($row[0], '[once]')) {
$row[0] = substr($row[0], 6);
$one_timer = TRUE;
}
# Here's the expansion loop
$pod->reset();
while ($pod->fetch()) {
$ndata[] = array_map(function($c) use($pod) {
return $pod->do_magic_tags($c);
}, $row);
$rvis[] = $orig_rvis[$row_idx];
if ($one_timer) { break; }
}
} # for each row of the original table
} else { # pod_dump is specified.
$flds = $pod->fields();
$hrow = array('ID','Date','Title','Author','Permalink');
$cvis = array(TRUE, TRUE, TRUE, TRUE, TRUE);
$frow = array();
foreach ($flds as $fkey => $finfo) {
$hrow[] = $finfo['label'];
$cvis[] = TRUE;
$frow[] = $fkey;
}
$ndata[] = $hrow;
$rvis = array(TRUE);
while ($pod->fetch()) {
$nrow = array();
$nrow[] = $pod->field('id');
$nrow[] = $pod->field('post_date');
$nrow[] = $pod->field('title');
$nrow[] = get_the_author_meta('display_name', $pod->field('author'));
$nrow[] = $pod->field('permalink');
foreach ($frow as $field_slug) {
$nrow[] = $pod->display($field_slug);
}
$ndata[] = $nrow;
$rvis[] = TRUE;
}
} # do a pod dump
# OK, install the new table information
$table['data'] = $ndata;
$table['visibility']['rows'] = $rvis;
$table['visibility']['columns'] = $cvis;
return $table;
}