class.jetpack-modules-list-table.php
| 1 | <?php |
| 2 | |
| 3 | if ( ! class_exists( 'WP_List_Table' ) ) |
| 4 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 5 | |
| 6 | class Jetpack_Modules_List_Table extends WP_List_Table { |
| 7 | |
| 8 | function __construct() { |
| 9 | parent::__construct(); |
| 10 | |
| 11 | Jetpack::init(); |
| 12 | |
| 13 | $this->items = $this->all_items = Jetpack_Admin::init()->get_modules(); |
| 14 | $this->items = $this->filter_displayed_table_items( $this->items ); |
| 15 | $this->items = apply_filters( 'jetpack_modules_list_table_items', $this->items ); |
| 16 | $this->_column_headers = array( $this->get_columns(), array(), array() ); |
| 17 | |
| 18 | wp_register_script( |
| 19 | 'models.jetpack-modules', |
| 20 | plugins_url( '_inc/jetpack-modules.models.js', JETPACK__PLUGIN_FILE ), |
| 21 | array( 'backbone', 'underscore' ), |
| 22 | JETPACK__VERSION |
| 23 | ); |
| 24 | wp_register_script( |
| 25 | 'views.jetpack-modules', |
| 26 | plugins_url( '_inc/jetpack-modules.views.js', JETPACK__PLUGIN_FILE ), |
| 27 | array( 'backbone', 'underscore', 'wp-util' ), |
| 28 | JETPACK__VERSION |
| 29 | ); |
| 30 | wp_register_script( |
| 31 | 'jetpack-modules-list-table', |
| 32 | plugins_url( '_inc/jetpack-modules.js', JETPACK__PLUGIN_FILE ), |
| 33 | array( |
| 34 | 'views.jetpack-modules', |
| 35 | 'models.jetpack-modules', |
| 36 | 'jquery', |
| 37 | ), |
| 38 | JETPACK__VERSION, |
| 39 | true |
| 40 | ); |
| 41 | |
| 42 | wp_localize_script( 'jetpack-modules-list-table', 'jetpackModulesData', array( |
| 43 | 'modules' => $this->all_items, |
| 44 | 'i18n' => array( |
| 45 | 'search_placeholder' => __( 'Search Modules…', 'jetpack' ), |
| 46 | ), |
| 47 | 'nonces' => array( |
| 48 | 'bulk' => wp_create_nonce( 'bulk-jetpack_page_jetpack_modules' ), |
| 49 | ), |
| 50 | ) ); |
| 51 | |
| 52 | wp_enqueue_script( 'jetpack-modules-list-table' ); |
| 53 | add_action( 'admin_footer', array( $this, 'js_templates' ), 9 ); |
| 54 | } |
| 55 | |
| 56 | function js_templates() { |
| 57 | ?> |
| 58 | <script type="text/html" id="tmpl-Jetpack_Modules_List_Table_Template"> |
| 59 | <# var i = 0; |
| 60 | if ( data.items.length ) { |
| 61 | _.each( data.items, function( item, key, list ) { |
| 62 | if ( item === undefined ) return; |
| 63 | #> |
| 64 | <tr class="jetpack-module <# if ( ++i % 2 ) { #> alternate<# } #><# if ( item.activated ) { #> active<# } #><# if ( ! item.available ) { #> unavailable<# } #>" id="{{{ item.module }}}"> |
| 65 | <th scope="row" class="check-column"> |
| 66 | <input type="checkbox" name="modules[]" value="{{{ item.module }}}" /> |
| 67 | </th> |
| 68 | <td class='name column-name'> |
| 69 | <span class='info'><a href="#">{{{ item.name }}}</a></span> |
| 70 | <div class="row-actions"> |
| 71 | <# if ( item.configurable ) { #> |
| 72 | <span class='configure'>{{{ item.configurable }}}</span> |
| 73 | <# } #> |
| 74 | <# if ( item.activated && 'vaultpress' !== item.module ) { #> |
| 75 | <span class='delete'><a href="<?php echo admin_url( 'admin.php' ); ?>?page=jetpack&action=deactivate&module={{{ item.module }}}&_wpnonce={{{ item.deactivate_nonce }}}"><?php _e( 'Deactivate', 'jetpack' ); ?></a></span> |
| 76 | <# } else if ( item.available ) { #> |
| 77 | <span class='activate'><a href="<?php echo admin_url( 'admin.php' ); ?>?page=jetpack&action=activate&module={{{ item.module }}}&_wpnonce={{{ item.activate_nonce }}}"><?php _e( 'Activate', 'jetpack' ); ?></a></span> |
| 78 | <# } #> |
| 79 | </div> |
| 80 | </td> |
| 81 | </tr> |
| 82 | <# |
| 83 | }); |
| 84 | } else { |
| 85 | #> |
| 86 | <tr class="no-modules-found"> |
| 87 | <td colspan="2"><?php esc_html_e( 'No Modules Found' , 'jetpack' ); ?></td> |
| 88 | </tr> |
| 89 | <# |
| 90 | } |
| 91 | #> |
| 92 | </script> |
| 93 | <?php |
| 94 | } |
| 95 | |
| 96 | function get_views() { |
| 97 | $modules = Jetpack_Admin::init()->get_modules(); |
| 98 | $array_of_module_tags = wp_list_pluck( $modules, 'module_tags' ); |
| 99 | $module_tags = call_user_func_array( 'array_merge', $array_of_module_tags ); |
| 100 | $module_tags_unique = array_count_values( $module_tags ); |
| 101 | ksort( $module_tags_unique ); |
| 102 | |
| 103 | $format = '<a href="%3$s"%4$s data-title="%1$s">%1$s <span class="count">(%2$s)</span></a>'; |
| 104 | $title = __( 'All', 'jetpack' ); |
| 105 | $count = count( $modules ); |
| 106 | $url = esc_url( remove_query_arg( 'module_tag' ) ); |
| 107 | $current = empty( $_GET['module_tag'] ) ? ' class="current all"' : ' class="all"'; |
| 108 | $views = array( |
| 109 | 'all' => sprintf( $format, $title, $count, $url, $current ), |
| 110 | ); |
| 111 | foreach ( $module_tags_unique as $title => $count ) { |
| 112 | $key = sanitize_title( $title ); |
| 113 | $display_title = esc_html( wptexturize( $title ) ); |
| 114 | $url = esc_url( add_query_arg( 'module_tag', urlencode( $title ) ) ); |
| 115 | $current = ''; |
| 116 | if ( ! empty( $_GET['module_tag'] ) && $title == $_GET['module_tag'] ) |
| 117 | $current = ' class="current"'; |
| 118 | $views[ $key ] = sprintf( $format, $display_title, $count, $url, $current ); |
| 119 | } |
| 120 | return $views; |
| 121 | } |
| 122 | |
| 123 | function views() { |
| 124 | $views = $this->get_views(); |
| 125 | |
| 126 | echo "<ul class='subsubsub'>\n"; |
| 127 | foreach ( $views as $class => $view ) { |
| 128 | $views[ $class ] = "\t<li class='$class'>$view</li>"; |
| 129 | } |
| 130 | echo implode( "\n", $views ) . "\n"; |
| 131 | echo "</ul>"; |
| 132 | } |
| 133 | |
| 134 | function filter_displayed_table_items( $modules ) { |
| 135 | return array_filter( $modules, array( $this, 'is_module_displayed' ) ); |
| 136 | } |
| 137 | |
| 138 | static function is_module_available( $module ) { |
| 139 | if ( ! is_array( $module ) || empty( $module ) ) |
| 140 | return false; |
| 141 | |
| 142 | return ! ( $module['requires_connection'] && ! Jetpack::is_active() ); |
| 143 | } |
| 144 | |
| 145 | static function is_module_displayed( $module ) { |
| 146 | // Handle module tag based filtering. |
| 147 | if ( ! empty( $_REQUEST['module_tag'] ) ) { |
| 148 | $module_tag = sanitize_text_field( $_REQUEST['module_tag'] ); |
| 149 | if ( ! in_array( $module_tag, $module['module_tags'] ) ) |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | // If nothing rejected it, include it! |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | static function sort_requires_connection_last( $module1, $module2 ) { |
| 158 | if ( $module1['requires_connection'] == $module2['requires_connection'] ) |
| 159 | return 0; |
| 160 | if ( $module1['requires_connection'] ) |
| 161 | return 1; |
| 162 | if ( $module2['requires_connection'] ) |
| 163 | return -1; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | function get_columns() { |
| 169 | $columns = array( |
| 170 | 'cb' => '<input type="checkbox" />', |
| 171 | 'name' => __( 'Name', 'jetpack' ), |
| 172 | ); |
| 173 | return $columns; |
| 174 | } |
| 175 | |
| 176 | function get_bulk_actions() { |
| 177 | $actions = array( |
| 178 | 'bulk-activate' => __( 'Activate', 'jetpack' ), |
| 179 | 'bulk-deactivate' => __( 'Deactivate', 'jetpack' ), |
| 180 | ); |
| 181 | return $actions; |
| 182 | } |
| 183 | |
| 184 | function single_row( $item ) { |
| 185 | static $i = 0; |
| 186 | $row_class = ( ++$i % 2 ) ? ' alternate' : ''; |
| 187 | |
| 188 | if ( ! empty( $item['activated'] ) ) |
| 189 | $row_class .= ' active'; |
| 190 | |
| 191 | if ( ! $this->is_module_available( $item ) ) |
| 192 | $row_class .= ' unavailable'; |
| 193 | |
| 194 | echo '<tr class="jetpack-module' . esc_attr( $row_class ) . '" id="' . esc_attr( $item['module'] ) . '">'; |
| 195 | $this->single_row_columns( $item ); |
| 196 | echo '</tr>'; |
| 197 | } |
| 198 | |
| 199 | function get_table_classes() { |
| 200 | return array( 'table', 'table-bordered', 'wp-list-table', 'widefat', 'fixed', 'jetpack-modules' ); |
| 201 | } |
| 202 | |
| 203 | function column_cb( $item ) { |
| 204 | if ( ! $this->is_module_available( $item ) ) |
| 205 | return ''; |
| 206 | |
| 207 | return sprintf( '<input type="checkbox" name="modules[]" value="%s" />', $item['module'] ); |
| 208 | } |
| 209 | |
| 210 | function column_icon( $item ) { |
| 211 | $badge_text = $free_text = ''; |
| 212 | ob_start(); |
| 213 | ?> |
| 214 | <a href="#TB_inline?width=600&height=550&inlineId=more-info-module-settings-modal" class="thickbox"> |
| 215 | <div class="module-image"> |
| 216 | <p><span class="module-image-badge"><?php echo $badge_text; ?></span><span class="module-image-free" style="display: none"><?php echo $free_text; ?></span></p> |
| 217 | </div> |
| 218 | </a> |
| 219 | <?php |
| 220 | return ob_get_clean(); |
| 221 | |
| 222 | } |
| 223 | |
| 224 | function column_name( $item ) { |
| 225 | $actions = array( |
| 226 | 'info' => sprintf( '<a href="%s">%s</a>', esc_url( '#' ), esc_html__( 'Module Info', 'jetpack' ) ), |
| 227 | ); |
| 228 | |
| 229 | if ( ! empty( $item['configurable'] ) ) { |
| 230 | $actions['configure'] = $item['configurable']; |
| 231 | } |
| 232 | |
| 233 | if ( empty( $item['activated'] ) && $this->is_module_available( $item ) ) { |
| 234 | $url = wp_nonce_url( |
| 235 | Jetpack::admin_url( array( |
| 236 | 'page' => 'jetpack', |
| 237 | 'action' => 'activate', |
| 238 | 'module' => $item['module'], |
| 239 | ) ), |
| 240 | 'jetpack_activate-' . $item['module'] |
| 241 | ); |
| 242 | $actions['activate'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html__( 'Activate', 'jetpack' ) ); |
| 243 | } elseif ( ! empty( $item['activated'] ) ) { |
| 244 | $url = wp_nonce_url( |
| 245 | Jetpack::admin_url( array( |
| 246 | 'page' => 'jetpack', |
| 247 | 'action' => 'deactivate', |
| 248 | 'module' => $item['module'], |
| 249 | ) ), |
| 250 | 'jetpack_deactivate-' . $item['module'] |
| 251 | ); |
| 252 | $actions['delete'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html__( 'Deactivate', 'jetpack' ) ); |
| 253 | } |
| 254 | |
| 255 | return $this->row_actions( $actions ) . wptexturize( $item['name'] ); |
| 256 | } |
| 257 | |
| 258 | function column_description( $item ) { |
| 259 | ob_start(); |
| 260 | echo apply_filters( 'jetpack_short_module_description', $item['description'], $item['module'] ); |
| 261 | do_action( 'jetpack_learn_more_button_' . $item['module'] ); |
| 262 | echo '<div id="more-info-' . $item['module'] . '" class="more-info">'; |
| 263 | if ( Jetpack::is_active() && has_action( 'jetpack_module_more_info_connected_' . $item['module'] ) ) { |
| 264 | do_action( 'jetpack_module_more_info_connected_' . $item['module'] ); |
| 265 | } else { |
| 266 | do_action( 'jetpack_module_more_info_' . $item['module'] ); |
| 267 | } |
| 268 | echo '</div>'; |
| 269 | return ob_get_clean(); |
| 270 | } |
| 271 | |
| 272 | function column_module_tags( $item ) { |
| 273 | $module_tags = array(); |
| 274 | foreach( $item['module_tags'] as $module_tag ) { |
| 275 | $module_tags[] = sprintf( '<a href="%3$s" data-title="%2$s">%1$s</a>', esc_html( $module_tag ), esc_attr( $module_tag ), esc_url( add_query_arg( 'module_tag', urlencode( $module_tag ) ) ) ); |
| 276 | } |
| 277 | return implode( ', ', $module_tags ); |
| 278 | } |
| 279 | |
| 280 | function column_default( $item, $column_name ) { |
| 281 | switch ( $column_name ) { |
| 282 | case 'icon': |
| 283 | case 'name': |
| 284 | case 'description': |
| 285 | break; |
| 286 | default: |
| 287 | return print_r( $item, true ); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Core switched their `display_tablenav()` method to protected, so we can't access it directly. |
| 293 | * Instead, let's include an access function to make it doable without errors! |
| 294 | * |
| 295 | * @see https://github.com/WordPress/WordPress/commit/d28f6344de97616de8ece543ed290c4ba2383622 |
| 296 | * |
| 297 | * @param string $which |
| 298 | * |
| 299 | * @return mixed |
| 300 | */ |
| 301 | function unprotected_display_tablenav( $which = 'top' ) { |
| 302 | return $this->display_tablenav( $which ); |
| 303 | } |
| 304 | |
| 305 | } |
| 306 |