challenge
1 year ago
css
1 week ago
img
1 year ago
js
1 month ago
menu
1 month ago
partials
2 weeks ago
rest-api
1 week ago
scss
1 year ago
settings
1 week ago
uninstall
1 year ago
wpchill
1 month ago
admin-notices.php
6 months ago
admin.php
1 month ago
class-strong-testimonials-addons.php
1 month ago
class-strong-testimonials-admin-category-list.php
1 year ago
class-strong-testimonials-admin-list.php
1 year ago
class-strong-testimonials-admin-scripts.php
1 month ago
class-strong-testimonials-admin.php
1 month ago
class-strong-testimonials-debug.php
5 months ago
class-strong-testimonials-exporter.php
1 year ago
class-strong-testimonials-help.php
1 year ago
class-strong-testimonials-helper.php
1 month ago
class-strong-testimonials-list-table.php
1 year ago
class-strong-testimonials-lite-vs-pro-page.php
1 month ago
class-strong-testimonials-post-editor.php
7 months ago
class-strong-testimonials-review.php
1 year ago
class-strong-testimonials-updater.php
1 month ago
class-strong-testimonials-upsell.php
1 week ago
class-strong-views-list-table.php
1 month ago
class-walker-strong-category-checklist.php
1 year ago
class-walker-strong-form-category-checklist.php
1 year ago
class-wpmtst-onboarding.php
1 year ago
compat.php
1 year ago
custom-fields-ajax.php
1 year ago
custom-fields.php
1 week ago
form-preview.php
1 year ago
view-list-order.php
1 year ago
views-ajax.php
1 month ago
views-validate.php
1 year ago
views.php
1 month ago
class-strong-views-list-table.php
287 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin List Table |
| 4 | * |
| 5 | * @version 0.2.1 |
| 6 | */ |
| 7 | |
| 8 | class Strong_Views_List_Table extends Strong_Testimonials_List_Table { |
| 9 | |
| 10 | public $stickies; |
| 11 | |
| 12 | /** |
| 13 | * Message to be displayed when there are no items |
| 14 | * |
| 15 | * @since 0.2.1 |
| 16 | * @access public |
| 17 | */ |
| 18 | public function no_items() { |
| 19 | esc_html_e( 'No views found.', 'strong-testimonials' ); |
| 20 | } |
| 21 | |
| 22 | public function prepare_list( $data = array() ) { |
| 23 | $this->stickies = get_option( 'wpmtst_sticky_views', array() ); |
| 24 | |
| 25 | $columns = $this->get_columns(); |
| 26 | $hidden = $this->get_hidden_columns(); |
| 27 | $sortable = $this->get_sortable_columns(); |
| 28 | |
| 29 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 30 | |
| 31 | // Sort the list |
| 32 | if ( isset( $_GET['orderby'] ) ) { |
| 33 | usort( $data, array( &$this, 'usort_reorder' ) ); |
| 34 | } |
| 35 | $data = $this->move_sticky( $data ); |
| 36 | $data = apply_filters( 'wpmtst_list_views', $data ); |
| 37 | if ( isset( $_GET['mode'] ) && ! empty( $_GET['mode'] ) && 'all' !== $_GET['mode'] ) { |
| 38 | $data = $this->filter_data( sanitize_text_field( wp_unslash( $_GET['mode'] ) ), $data ); |
| 39 | } |
| 40 | if ( isset( $_GET['s'] ) && ! empty( $_GET['s'] ) ) { |
| 41 | $data = $this->search_data( sanitize_text_field( wp_unslash( $_GET['s'] ) ), $data ); |
| 42 | } |
| 43 | $this->items = $data; |
| 44 | } |
| 45 | |
| 46 | public function prepare_filters( $data = array() ) { |
| 47 | $links = array(); |
| 48 | foreach ( $data as $item ) { |
| 49 | $value = unserialize( $item['value'] ); |
| 50 | $links[ $value['mode'] ][] = $item; |
| 51 | } |
| 52 | return $links; |
| 53 | } |
| 54 | |
| 55 | public function filter_data( $mode, $data = array() ) { |
| 56 | $items = array(); |
| 57 | foreach ( $data as $item ) { |
| 58 | if ( $mode === $item['data']['mode'] ) { |
| 59 | $items[] = $item; |
| 60 | } |
| 61 | } |
| 62 | return $items; |
| 63 | } |
| 64 | |
| 65 | public function search_data( $search, $data = array() ) { |
| 66 | $items = array(); |
| 67 | foreach ( $data as $item ) { |
| 68 | if ( strtolower( $search ) === strtolower( $item['name'] ) ) { |
| 69 | $items[] = $item; |
| 70 | } |
| 71 | } |
| 72 | return $items; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Move sticky views to the top |
| 77 | * |
| 78 | * @param $data |
| 79 | * @since 0.2.0 |
| 80 | * @return array |
| 81 | */ |
| 82 | public function move_sticky( $data ) { |
| 83 | $sticky_views = array(); |
| 84 | $views = array(); |
| 85 | foreach ( $data as $view ) { |
| 86 | if ( in_array( $view['id'], $this->stickies, true ) ) { |
| 87 | $sticky_views[] = $view; |
| 88 | } else { |
| 89 | $views[] = $view; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return array_merge( $sticky_views, $views ); |
| 94 | } |
| 95 | |
| 96 | public function get_columns() { |
| 97 | $columns = array( |
| 98 | 'id' => esc_html__( 'ID', 'strong-testimonials' ), |
| 99 | //'sticky' => __( 'Sticky', 'strong-testimonials' ), |
| 100 | 'sticky' => '', |
| 101 | 'name' => esc_html__( 'Name', 'strong-testimonials' ), |
| 102 | 'mode' => esc_html__( 'Mode', 'strong-testimonials' ), |
| 103 | 'template' => esc_html__( 'Template', 'strong-testimonials' ), |
| 104 | 'shortcode' => esc_html__( 'Shortcode', 'strong-testimonials' ), |
| 105 | ); |
| 106 | |
| 107 | return $columns; |
| 108 | } |
| 109 | |
| 110 | public function get_hidden_columns() { |
| 111 | return array(); |
| 112 | } |
| 113 | |
| 114 | public function get_sortable_columns() { |
| 115 | return array( |
| 116 | 'id' => array( 'id', false ), |
| 117 | 'name' => array( 'name', false ), |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | public function usort_reorder( $a, $b ) { |
| 122 | // If no sort, default to name |
| 123 | $orderby = ( ! empty( $_GET['orderby'] ) ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : 'name'; |
| 124 | |
| 125 | // If no order, default to asc |
| 126 | $order = ( ! empty( $_GET['order'] ) ) ? sanitize_text_field( wp_unslash( $_GET['order'] ) ) : 'asc'; |
| 127 | |
| 128 | // Determine sort order |
| 129 | if ( 'id' === $orderby ) { |
| 130 | $result = $this->cmp( intval( $a[ $orderby ] ), intval( $b[ $orderby ] ) ); |
| 131 | } else { |
| 132 | $result = strcasecmp( $a[ $orderby ], $b[ $orderby ] ); |
| 133 | } |
| 134 | |
| 135 | // Send final sort direction to usort |
| 136 | return ( 'asc' === $order ) ? $result : -$result; |
| 137 | } |
| 138 | |
| 139 | public function cmp( $a, $b ) { |
| 140 | if ( (int) $a === (int) $b ) { |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | return ( $a < $b ) ? -1 : 1; |
| 145 | } |
| 146 | |
| 147 | public function column_name( $item ) { |
| 148 | $screen = get_current_screen(); |
| 149 | $url = $screen->parent_file; |
| 150 | |
| 151 | // Edit link |
| 152 | $edit_link = esc_url( $url . '&page=testimonial-views&action=edit&id=' . $item['id'] ); |
| 153 | echo '<a class="row-title" href="' . esc_url( $edit_link ) . '">' . esc_html( $item['name'] ) . '</a>'; |
| 154 | |
| 155 | // Duplicate link |
| 156 | // @since 2.1.0 |
| 157 | $duplicate_link = esc_url( $url . '&page=testimonial-views&action=duplicate&id=' . $item['id'] ); |
| 158 | |
| 159 | // Delete link |
| 160 | $delete_link = 'admin.php?action=delete-strong-view&id=' . $item['id']; |
| 161 | |
| 162 | // Assemble links |
| 163 | |
| 164 | $default_views = get_option( 'wpmtst_default_views', array() ); |
| 165 | $protected_ids = array_filter( array( $default_views['single_template'] ?? null ) ); |
| 166 | $is_protected = in_array( (string) $item['id'], array_map( 'strval', $protected_ids ), true ); |
| 167 | |
| 168 | $actions = array(); |
| 169 | $actions['edit'] = '<a href="' . esc_url( $edit_link ) . '">' . esc_html__( 'Edit', 'strong-testimonials' ) . '</a>'; |
| 170 | |
| 171 | if ( ! $is_protected ) { |
| 172 | $actions['duplicate'] = '<a href="' . esc_url( $duplicate_link ) . '">' . esc_html__( 'Duplicate', 'strong-testimonials' ) . '</a>'; |
| 173 | // translators: %s is the name of the view that is going to be deleted. |
| 174 | $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( $delete_link, 'delete-strong-view_' . $item['id'] ) . "' onclick=\"if ( confirm( '" . esc_js( sprintf( __( 'Delete "%s"?', 'strong-testimonials' ), $item['name'] ) ) . "' ) ) { return true;} return false;\">" . esc_html__( 'Delete', 'strong-testimonials' ) . '</a>'; |
| 175 | } |
| 176 | |
| 177 | $actions = apply_filters( 'wpmtst_views_actions', $actions, $item ); |
| 178 | |
| 179 | echo wp_kses_post( $this->row_actions( $actions ) ); |
| 180 | } |
| 181 | |
| 182 | public function column_default( $item, $column_name ) { |
| 183 | switch ( $column_name ) { |
| 184 | case 'id': |
| 185 | $text = $item['id']; |
| 186 | break; |
| 187 | case 'sticky': |
| 188 | $stuck = $this->is_stuck( $item['id'] ) ? 'stuck' : ''; |
| 189 | $text = '<a href="#" class="stickit ' . $stuck . '" title="' . esc_html__( 'stick to top of list', 'strong-testimonials' ) . '"></>'; |
| 190 | break; |
| 191 | case 'name': |
| 192 | $text = $item['name']; |
| 193 | break; |
| 194 | case 'mode': |
| 195 | $mode = $item['data']['mode']; |
| 196 | $text = $mode; |
| 197 | if ( 'single_template' === $mode ) { |
| 198 | $text = esc_html__( 'Single testimonial template.', 'strong-testimonials' ); |
| 199 | } else { |
| 200 | $view_options = Strong_Testimonials_Defaults::get_view_options(); |
| 201 | if ( isset( $view_options['mode'][ $mode ]['label'] ) ) { |
| 202 | $text = $view_options['mode'][ $mode ]['label']; |
| 203 | } |
| 204 | } |
| 205 | break; |
| 206 | case 'template': |
| 207 | $text = $this->find_template( array( 'template' => $item['data']['template'] ) ); |
| 208 | break; |
| 209 | case 'shortcode': |
| 210 | if ( 'single_template' === $item['data']['mode'] ) { |
| 211 | $text = ''; |
| 212 | } else { |
| 213 | $text = '[testimonial_view id="' . $item['id'] . '"]'; |
| 214 | } |
| 215 | break; |
| 216 | default: |
| 217 | $text = print_r( $item, true ); |
| 218 | } |
| 219 | |
| 220 | return apply_filters( "wpmtst_view_list_column_$column_name", $text, $item ); |
| 221 | } |
| 222 | |
| 223 | public function is_stuck( $id ) { |
| 224 | $stickies = get_option( 'wpmtst_sticky_views', array() ); |
| 225 | return ( $stickies && in_array( $id, $stickies, true ) ); |
| 226 | } |
| 227 | |
| 228 | public function find_template( $atts = '' ) { |
| 229 | $name = WPMST()->templates->get_template_config( $atts, 'name', false ); |
| 230 | return $name ? $name : '<span class="error"><span class="dashicons dashicons-warning"></span> ' . esc_html__( 'not found', 'strong-testimonials' ) . '</span>'; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Display the table |
| 235 | * |
| 236 | * @since 3.1.0 |
| 237 | * @access public |
| 238 | */ |
| 239 | public function display() { |
| 240 | $singular = $this->_args['singular']; |
| 241 | // Disabling the table nav options to regain some real estate. |
| 242 | //$this->display_tablenav( 'top' ); |
| 243 | $s = ''; |
| 244 | if ( isset( $_GET['s'] ) ) { |
| 245 | $s = sanitize_text_field( wp_unslash( $_GET['s'] ) ); |
| 246 | } |
| 247 | ?> |
| 248 | <form id="posts-filter" method="get"> |
| 249 | <p class="search-box"> |
| 250 | <label class="screen-reader-text" for="post-search-input"><?php esc_html_e( 'Search', 'strong-testimonials' ); ?></label> |
| 251 | <input type="search" id="post-search-input" name="s" value="<?php echo esc_attr( $s ); ?>"> |
| 252 | <input type="submit" id="search-submit" class="button" value="<?php esc_html_e( 'Search', 'strong-testimonials' ); ?>"> |
| 253 | <input type="hidden" name="post_type" class="post_type_page" value="wpm-testimonial"> |
| 254 | <input type="hidden" name="page" value="testimonial-views"> |
| 255 | </p> |
| 256 | <table class="wp-list-table <?php echo esc_attr( implode( ' ', $this->get_table_classes() ) ); ?>"> |
| 257 | <thead> |
| 258 | <tr> |
| 259 | <?php $this->print_column_headers(); ?> |
| 260 | </tr> |
| 261 | </thead> |
| 262 | |
| 263 | <tbody id="the-list" |
| 264 | <?php |
| 265 | if ( $singular ) { |
| 266 | echo " data-wp-lists='list:" . esc_attr( $singular ) . "'"; |
| 267 | } |
| 268 | ?> |
| 269 | > |
| 270 | <?php $this->display_rows_or_placeholder(); ?> |
| 271 | </tbody> |
| 272 | |
| 273 | <tfoot> |
| 274 | <tr> |
| 275 | <?php $this->print_column_headers( false ); ?> |
| 276 | </tr> |
| 277 | </tfoot> |
| 278 | |
| 279 | </table> |
| 280 | <?php |
| 281 | //$this->display_tablenav( 'bottom' ); |
| 282 | ?> |
| 283 | </form> |
| 284 | <?php |
| 285 | } |
| 286 | } |
| 287 |