-
Notifications
You must be signed in to change notification settings - Fork 4
/
tablepress-v4.php
113 lines (100 loc) · 3.14 KB
/
tablepress-v4.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
102
103
104
105
106
107
108
109
110
111
112
113
<?php
class acf_field_tablepress extends acf_field {
var $settings,
$defaults;
function __construct() {
$this->name = 'tablepress_field';
$this->label = __('TablePress', 'acf-tablepress' );
$this->category = __("Relational",'acf');
$this->defaults = array(
'allow_null' => 0,
'return_format' => 'table_id'
);
parent::__construct();
}
function create_options( $field ) {
$field = array_merge($this->defaults, $field);
$key = $field['name'];
/* Create Field Options HTML */
?>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Allow Null?",'acf'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'radio',
'name' => 'fields['.$key.'][allow_null]',
'value' => $field['allow_null'],
'choices' => array(
1 => __("Yes",'acf'),
0 => __("No",'acf'),
),
'layout' => 'horizontal',
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Return Format?",'acf-tablepress'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'radio',
'name' => 'fields['.$key.'][return_format]',
'value' => $field['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',
));
?>
</td>
</tr>
<?php
}
function create_field( $field ) {
$field = array_merge($this->defaults, $field);
$choices = array();
/* 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 */
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 );
$field['choices'] = $choices;
$field['type'] = 'select';
do_action('acf/create_field', $field);
}
function format_value_for_api( $value, $post_id, $field ) {
$field = array_merge($this->defaults, $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();