/wp-content/plugins/codepress-admin-columns/classes/column/actions.php

https://gitlab.com/oxidigitaluser/liguelista · PHP · 189 lines · 86 code · 31 blank · 72 comment · 4 complexity · bdfe5b1a17869e22faa368e16b1d69c2 MD5 · raw file

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