This repository was archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtablepress-ext-shortcode-attrs-plus.php
More file actions
101 lines (89 loc) · 3.34 KB
/
Copy pathtablepress-ext-shortcode-attrs-plus.php
File metadata and controls
101 lines (89 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* @link https://github.com/ucsf-ckm/tablepress-ext-shortcode-attrs-plus
* @since 1.0.0
* @package TablePressExtShortcodeAttrsPlus
*
* @wordpress-plugin
* Plugin Name: TablePress Extension: Shortcode Attributes Plus
* Plugin URI: https://github.com/ucsf-ckm/tablepress-ext-shortcode-attrs-plus
* Description: Provides additional attributes to the [table /] shortcode.
* Version: 1.0.0
* Author: Stefan Topfstedt
* Author URI: https://github.com/stopfstedt
* License: MIT
* License URI: https://opensource.org/licenses/MIT
* GitHub Plugin URI: https://github.com/ucsf-ckm/tablepress-ext-shortcode-attrs-plus
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_filter( 'tablepress_datatables_parameters', 'tablepress_ext_shortcode_attrs_plus_datatables_parameters', 10, 5 );
add_filter( 'tablepress_shortcode_table_default_shortcode_atts', 'tablepress_ext_shortcode_attrs_plus_shortcode_table_default_shortcode_atts' );
add_filter( 'tablepress_table_js_options', 'tablepress_ext_shortcode_attrs_plus_js_options', 10, 3 );
/**
* Defines additional attributes to be used with the [table /] Shortcode.
*
* @since 1.0
*
* @param array $default_atts Default attributes for the [table /] shortcode.
*
* @return array Extended attributes for the Shortcode.
*/
function tablepress_ext_shortcode_attrs_plus_shortcode_table_default_shortcode_atts( $default_atts ) {
$default_atts['invisible-columns'] = '';
$default_atts['hide-search-box'] = false;
return $default_atts;
}
/**
* Adds column definitions and DOM overrides to the data-table config parameters, if applicable.
*
* @since 1.0.0
*
* @param array $parameters The parameters for the DataTables JS library.
* @param string $table_id The current table ID.
* @param string $html_id The ID of the table HTML element.
* @param array $js_options The options for the JS library.
*
* @return array the modified parameters array.
*/
function tablepress_ext_shortcode_attrs_plus_datatables_parameters( $parameters, $table_id, $html_id, $js_options ) {
// define columns as invisible
$invisible_columns = array();
if ( '' !== trim( $js_options['invisible_columns'] ) ) {
$invisible_columns = array_map( function ( $value ) {
return intval( trim( $value ), 10 );
}, explode( ',', $js_options['invisible_columns'] ) );
}
if ( ! empty( $invisible_columns ) ) {
$column_definitions = array(
array(
"targets" => $invisible_columns,
"visible" => false,
)
);
$parameters[] = '"columnDefs": ' . json_encode( $column_definitions );
}
// remove search box from DOM definition
$hide_search_box = filter_var( $js_options['hide_search_box'], FILTER_VALIDATE_BOOLEAN );
if ( $hide_search_box ) {
$parameters[] = '"sDom": "<\"H\"lr>t<\"F\"ip>"';
}
return $parameters;
}
/**
* Pass shortcode attributes to JavaScript arguments.
*
* @since 1.0
*
* @param array $js_options Current JS options.
* @param string $table_id Table ID.
* @param array $render_options Render Options.
*
* @return array Modified JS options.
*/
function tablepress_ext_shortcode_attrs_plus_js_options( $js_options, $table_id, $render_options ) {
$js_options['invisible_columns'] = $render_options['invisible-columns'];
$js_options['hide_search_box'] = $render_options['hide-search-box'];
return $js_options;
}