/
actions.php
192 lines (164 loc) · 5.01 KB
/
actions.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Base class for columns containing action links for items.
*
* @since 2.2.6
*/
abstract class CPAC_Column_Actions extends CPAC_Column {
/**
* Get a list of action links for an item (e.g. post) ID.
*
* @since 2.2.6
*
* @param int $id Item ID to get the list of actions for.
*
* @return array List of actions ([action name] => [action link]).
*/
abstract public function get_actions( $id );
/**
* @see CPAC_Column::init()
* @since 2.2.6
*/
public function init() {
parent::init();
// Properties
$this->properties['type'] = 'column-actions';
$this->properties['label'] = __( 'Actions', 'codepress-admin-columns' );
// Options
$this->options['use_icons'] = false;
}
/**
* @see CPAC_Column::get_value()
* @since 2.2.6
*/
public function get_value( $id ) {
$actions = $this->get_raw_value( $id );
if ( ! $actions ) {
return false;
}
if ( ! empty( $this->options->use_icons ) ) {
return implode( '', $this->convert_actions_to_icons( $actions ) );
}
$i = 0;
$num_actions = count( $actions );
foreach ( $actions as $class => $action ) {
$actions[ $class ] = '<span class="' . esc_attr( $class ) . '">' . $action . ( $i < $num_actions - 1 ? ' | ' : '' ) . '</span>';
$i ++;
}
return implode( '', $actions );
}
/**
* @see CPAC_Column::get_value()
* @since 2.2.6
*/
public function get_raw_value( $id ) {
/**
* Filter the action links for the actions column
*
* @since 2.2.9
*
* @param array $actions List of actions ([action name] => [action link]).
* @param CPAC_Column_Actions $column_instance Column object.
* @param int $id Post/User/Comment ID
*/
return apply_filters( 'cac/column/actions/action_links', $this->get_actions( $id ), $this, $id );
}
/**
* @see CPAC_Column::display_settings()
* @since 2.2.6
*/
public function display_settings() {
parent::display_settings();
$this->display_field_use_icons();
}
/**
* Display the settings field for using icons instead of text links.
*
* @since 2.2.6
*/
public function display_field_use_icons() {
?>
<tr class="column_editing">
<?php $this->label_view( __( 'Use icons?', 'codepress-admin-columns' ), __( 'Use icons instead of text for displaying the actions.', 'codepress-admin-columns' ), 'use_icons' ); ?>
<td class="input">
<label for="<?php $this->attr_id( 'use_icons' ); ?>-yes">
<input type="radio" value="1" name="<?php $this->attr_name( 'use_icons' ); ?>" id="<?php $this->attr_id( 'use_icons' ); ?>-yes"<?php checked( $this->options->use_icons, '1' ); ?> />
<?php _e( 'Yes' ); ?>
</label>
<label for="<?php $this->attr_id( 'use_icons' ); ?>-no">
<input type="radio" value="" name="<?php $this->attr_name( 'use_icons' ); ?>" id="<?php $this->attr_id( 'use_icons' ); ?>-no"<?php checked( $this->options->use_icons, '' ); ?> />
<?php _e( 'No' ); ?>
</label>
</td>
</tr>
<?php
}
/**
* Convert items from a list of action links to icons (if they have an icon).
*
* @since 2.2.6
*
* @param array $actions List of actions ([action name] => [action link]).
*
* @return array List of actions ([action name] => [action icon link]).
*/
public function convert_actions_to_icons( $actions ) {
$icons = $this->get_actions_icons();
foreach ( $actions as $action => $link ) {
$action1 = $action;
$spacepos = $spacepos = strpos( $action1, ' ' );
if ( $spacepos !== false ) {
$action1 = substr( $action1, 0, $spacepos );
}
if ( isset( $icons[ $action1 ] ) ) {
// Add mandatory "class" HTML attribute
if ( strpos( $link, 'class=' ) === false ) {
$link = str_replace( '<a ', '<a class="" ', $link );
}
// Add icon and tooltip classes
$link = preg_replace( '/class=["\'](.*?)["\']/', 'class="$1 cpac-tip button cpac-button-action dashicons hide-content dashicons-' . $icons[ $action1 ] . '"', $link, 1 );
// Add tooltip title
$link = preg_replace_callback( '/>(.*?)<\/a>/', array( $this, 'add_link_tooltip' ), $link );
$actions[ $action ] = $link;
}
}
return $actions;
}
/**
* Add the tooltip data attribute to the link
* Callback for preg_replace_callback
*
* @since 2.2.6.1
*
* @param array $matches Matches information from preg_replace_callback
*
* @return string Link part with tooltip attribute
*/
public function add_link_tooltip( $matches ) {
return ' data-tip="' . esc_attr( $matches[1] ) . '">' . $matches[1] . '</a>';
}
/**
* Get a list of action names and their corresponding dashicons.
*
* @since 2.2.6
*
* @return array List of actions and icons ([action] => [dashicon]).
*/
public function get_actions_icons() {
return array(
'edit' => 'edit',
'trash' => 'trash',
'delete' => 'trash',
'untrash' => 'undo',
'unspam' => 'undo',
'view' => 'visibility',
'inline' => 'welcome-write-blog',
'quickedit' => 'welcome-write-blog',
'approve' => 'yes',
'unapprove' => 'no',
'reply' => 'testimonial',
'trash' => 'trash',
'spam' => 'welcome-comments'
);
}
}