comment
10 years ago
link
10 years ago
media
10 years ago
post
10 years ago
user
10 years ago
acf-placeholder.php
10 years ago
actions.php
10 years ago
custom-field.php
10 years ago
default.php
10 years ago
taxonomy.php
10 years ago
used-by-menu.php
10 years ago
wc-placeholder.php
10 years ago
actions.php
189 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Base class for columns containing action links for items. |
| 4 | * |
| 5 | * @since 2.2.6 |
| 6 | */ |
| 7 | abstract class CPAC_Column_Actions extends CPAC_Column { |
| 8 | |
| 9 | /** |
| 10 | * Get a list of action links for an item (e.g. post) ID. |
| 11 | * |
| 12 | * @since 2.2.6 |
| 13 | * |
| 14 | * @param int $id Item ID to get the list of actions for. |
| 15 | * @return array List of actions ([action name] => [action link]). |
| 16 | */ |
| 17 | abstract public function get_actions( $id ); |
| 18 | |
| 19 | /** |
| 20 | * @see CPAC_Column::init() |
| 21 | * @since 2.2.6 |
| 22 | */ |
| 23 | public function init() { |
| 24 | |
| 25 | parent::init(); |
| 26 | |
| 27 | // Properties |
| 28 | $this->properties['type'] = 'column-actions'; |
| 29 | $this->properties['label'] = __( 'Actions', 'codepress-admin-columns' ); |
| 30 | |
| 31 | // Options |
| 32 | $this->options['use_icons'] = false; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @see CPAC_Column::get_value() |
| 37 | * @since 2.2.6 |
| 38 | */ |
| 39 | public function get_value( $id ) { |
| 40 | |
| 41 | $actions = $this->get_raw_value( $id ); |
| 42 | |
| 43 | if ( ! empty( $this->options->use_icons ) ) { |
| 44 | return implode( '', $this->convert_actions_to_icons( $actions ) ); |
| 45 | } |
| 46 | |
| 47 | $i = 0; |
| 48 | $num_actions = count( $actions ); |
| 49 | |
| 50 | foreach ( $actions as $class => $action ) { |
| 51 | $actions[ $class ] = '<span class="' . esc_attr( $class ) . '">' . $action . ( $i < $num_actions - 1 ? ' | ' : '' ) . '</span>'; |
| 52 | $i++; |
| 53 | } |
| 54 | |
| 55 | return implode( '', $actions ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @see CPAC_Column::get_value() |
| 60 | * @since 2.2.6 |
| 61 | */ |
| 62 | public function get_raw_value( $id ) { |
| 63 | |
| 64 | /** |
| 65 | * Filter the action links for the actions column |
| 66 | * |
| 67 | * @since 2.2.9 |
| 68 | * |
| 69 | * @param array $actions List of actions ([action name] => [action link]). |
| 70 | * @param CPAC_Column_Actions $column_instance Column object. |
| 71 | * @param int $id Post/User/Comment ID |
| 72 | */ |
| 73 | return apply_filters( 'cac/column/actions/action_links', $this->get_actions( $id ), $this, $id ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @see CPAC_Column::display_settings() |
| 78 | * @since 2.2.6 |
| 79 | */ |
| 80 | public function display_settings() { |
| 81 | |
| 82 | parent::display_settings(); |
| 83 | |
| 84 | $this->display_field_use_icons(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Display the settings field for using icons instead of text links. |
| 89 | * |
| 90 | * @since 2.2.6 |
| 91 | */ |
| 92 | public function display_field_use_icons() { |
| 93 | |
| 94 | ?> |
| 95 | <tr class="column_editing"> |
| 96 | <?php $this->label_view( __( 'Use icons?', 'codepress-admin-columns' ), __( 'Use icons instead of text for displaying the actions.', 'codepress-admin-columns' ), 'use_icons' ); ?> |
| 97 | <td class="input"> |
| 98 | <label for="<?php $this->attr_id( 'use_icons' ); ?>-yes"> |
| 99 | <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' ); ?> /> |
| 100 | <?php _e( 'Yes'); ?> |
| 101 | </label> |
| 102 | <label for="<?php $this->attr_id( 'use_icons' ); ?>-no"> |
| 103 | <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, '' ); ?> /> |
| 104 | <?php _e( 'No'); ?> |
| 105 | </label> |
| 106 | </td> |
| 107 | </tr> |
| 108 | <?php |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Convert items from a list of action links to icons (if they have an icon). |
| 113 | * |
| 114 | * @since 2.2.6 |
| 115 | * |
| 116 | * @param array $actions List of actions ([action name] => [action link]). |
| 117 | * @return array List of actions ([action name] => [action icon link]). |
| 118 | */ |
| 119 | public function convert_actions_to_icons( $actions ) { |
| 120 | |
| 121 | $icons = $this->get_actions_icons(); |
| 122 | |
| 123 | foreach ( $actions as $action => $link ) { |
| 124 | $action1 = $action; |
| 125 | $spacepos = $spacepos = strpos( $action1, ' ' ); |
| 126 | |
| 127 | if ( $spacepos !== false ) { |
| 128 | $action1 = substr( $action1, 0, $spacepos ); |
| 129 | } |
| 130 | |
| 131 | if ( isset( $icons[ $action1 ] ) ) { |
| 132 | // Add mandatory "class" HTML attribute |
| 133 | if ( strpos( $link, 'class=' ) === false ) { |
| 134 | $link = str_replace( '<a ', '<a class="" ', $link ); |
| 135 | } |
| 136 | |
| 137 | // Add icon and tooltip classes |
| 138 | $link = preg_replace( '/class=["\'](.*?)["\']/', 'class="$1 cpac-tip button cpac-button-action dashicons hide-content dashicons-' . $icons[ $action1 ] . '"', $link, 1 ); |
| 139 | |
| 140 | // Add tooltip title |
| 141 | $link = preg_replace_callback( '/>(.*?)<\/a>/', array( $this, 'add_link_tooltip' ), $link ); |
| 142 | |
| 143 | $actions[ $action ] = $link; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return $actions; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Add the tooltip data attribute to the link |
| 152 | * Callback for preg_replace_callback |
| 153 | * |
| 154 | * @since 2.2.6.1 |
| 155 | * |
| 156 | * @param array $matches Matches information from preg_replace_callback |
| 157 | * @return string Link part with tooltip attribute |
| 158 | */ |
| 159 | public function add_link_tooltip( $matches ) { |
| 160 | return ' data-tip="' . esc_attr( $matches[1] ) . '">' . $matches[1] . '</a>'; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get a list of action names and their corresponding dashicons. |
| 165 | * |
| 166 | * @since 2.2.6 |
| 167 | * |
| 168 | * @return array List of actions and icons ([action] => [dashicon]). |
| 169 | */ |
| 170 | public function get_actions_icons() { |
| 171 | |
| 172 | return array( |
| 173 | 'edit' => 'edit', |
| 174 | 'trash' => 'trash', |
| 175 | 'delete' => 'trash', |
| 176 | 'untrash' => 'undo', |
| 177 | 'unspam' => 'undo', |
| 178 | 'view' => 'visibility', |
| 179 | 'inline' => 'welcome-write-blog', |
| 180 | 'quickedit' => 'welcome-write-blog', |
| 181 | 'approve' => 'yes', |
| 182 | 'unapprove' => 'no', |
| 183 | 'reply' => 'testimonial', |
| 184 | 'trash' => 'trash', |
| 185 | 'spam' => 'welcome-comments' |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | } |