-
Notifications
You must be signed in to change notification settings - Fork 4
/
tablepress-v5.php
101 lines (96 loc) · 3.18 KB
/
tablepress-v5.php
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
class acf_field_tablepress extends acf_field {
function __construct() {
$this->name = 'tablepress_field';
$this->label = __('TablePress');
$this->category = __("Relational",'acf');
$this->defaults = array(
'allow_null' => 0,
'return_format' => 'table_id'
);
parent::__construct();
}
function render_field_settings( $field ) {
$field_settings_params = array(
array(
'label' => 'Allow Null?',
'type' => 'radio',
'name' => 'allow_null',
'choices' => array(
1 => __("Yes",'acf'),
0 => __("No",'acf'),
),
'layout' => 'horizontal'
),
array(
'label' => __('Return Format','acf-tablepress'),
'instructions' => '',
'type' => 'radio',
'name' => 'return_format',
'choices' => array(
'table_id' => __("Table ID - Output only the Table ID Number",'acf-tablepress'),
'rendered_html' => __("HTML - Output the rendered HTML of the table itself. Equivalent to do_shortcode(), but does not use that function.",'acf-tablepress'),
),
'layout' => 'vertical'
)
);
foreach ($field_settings_params as $field_settings) {
acf_render_field_setting( $field, $field_settings);
}
}
function render_field( $field ) {
/* Exits function if TablePress not active */
if ( !defined( 'TABLEPRESS_ABSPATH' ) ) {
echo __('TablePress must be activated for this ACF field to work', 'acf-tablepress');
return;
}
/* get list of table ID and post ID pairs */
$table_json = get_option( 'tablepress_tables' );
$json_dec = json_decode( $table_json, true );
$tables = $json_dec['table_post'];
/* Get table titles for list of choices */
$choices = array();
if ( !is_array( $tables ) || empty( $tables ) ) {
echo sprintf( __('No TablePress tables found, once you <a href="%s">add some tables</a> they\'ll show up here.', 'acf-tablepress' ), admin_url( 'admin.php?page=tablepress' ) );
return;
}
foreach ($tables as $table_id => $post_id) {
$post = get_post( $post_id );
$choices[ $table_id ] = $post->post_title;
}
asort( $choices );
/* override field settings and render */
$field['choices'] = $choices;
$field['type'] = 'select';
?>
<select id="<?php echo str_replace(array('[',']'), array('-',''), $field['name']);?>" name="<?php echo $field['name']; ?>">
<?php
if ( $field['allow_null'] ) echo '<option value="">- '. __('Select', 'acf-tablepress') .' -</option>';
foreach ($field['choices'] as $key => $value) :
$selected = '';
if ( (is_array($field['value']) && in_array($key, $field['value'])) || $field['value'] == $key )
$selected = ' selected="selected"';
?>
<option value="<?php echo $key; ?>"<?php echo $selected;?>><?php echo $value; ?></option>
<?php
endforeach;
?>
</select>
<?php
}
function format_value( $value, $post_id, $field ) {
if ( $field['return_format'] == 'table_id' ) {
return $value;
}
if ( $field['return_format'] == 'rendered_html' ) {
if ( !function_exists( 'tablepress_get_table' ) ) {
return 'TablePress must be enabled';
}
$value = tablepress_get_table( array(
'id' => $value,
));
return $value;
}
}
} //End of Class
new acf_field_tablepress();