/** * Prepares the items to later display in the table. * Should run before any headers are sent. */ function prepare_items() { global $status, $snippets, $totals, $page, $orderby, $order, $s; wp_reset_vars(array('orderby', 'order', 's')); $screen = get_current_screen(); $user = get_current_user_id(); /* First, lets process the bulk actions */ $this->process_bulk_actions(); $snippets = array('all' => apply_filters('code_snippets/list_table/get_snippets', get_snippets($screen->is_network)), 'active' => array(), 'inactive' => array(), 'recently_activated' => array(), 'admin' => array(), 'frontend' => array()); /* Filter snippets by tag */ if (isset($_POST['tag'])) { $location = empty($_POST['tag']) ? remove_query_arg('tag') : add_query_arg('tag', $_POST['tag']); wp_redirect(esc_url_raw($location)); } if (!empty($_GET['tag'])) { $snippets['all'] = array_filter($snippets['all'], array($this, '_tags_filter_callback')); } /* Filter snippets based on search query */ if ($s) { $snippets['all'] = array_filter($snippets['all'], array($this, '_search_callback')); } if ($screen->is_network) { $recently_activated = get_site_option('recently_activated_snippets', array()); } else { $recently_activated = get_option('recently_activated_snippets', array()); } $one_week = 7 * 24 * 60 * 60; foreach ($recently_activated as $key => $time) { if ($time + $one_week < time()) { unset($recently_activated[$key]); } } if ($screen->is_network) { update_site_option('recently_activated_snippets', $recently_activated); } else { update_option('recently_activated_snippets', $recently_activated); } $scopes_enabled = code_snippets_get_setting('general', 'snippet_scope_enabled'); foreach ((array) $snippets['all'] as $snippet) { /* Filter into individual sections */ if ($snippet->active) { $snippets['active'][] = $snippet; } else { // Was the snippet recently activated? if (isset($recently_activated[$snippet->id])) { $snippets['recently_activated'][] = $snippet; } $snippets['inactive'][] = $snippet; } if ($scopes_enabled) { if ('1' == $snippet->scope) { $snippets['admin'][] = $snippet; } elseif ('2' == $snippet->scope) { $snippets['frontend'][] = $snippet; } } } $totals = array(); foreach ($snippets as $type => $list) { $totals[$type] = count($list); } if (empty($snippets[$status])) { $status = 'all'; } $data = $snippets[$status]; /* * First, lets decide how many records per page to show * by getting the user's setting in the Screen Options * panel. */ $sort_by = $screen->get_option('per_page', 'option'); $screen_option = $screen->get_option('per_page', 'option'); $per_page = get_user_meta($user, $screen_option, true); if (empty($per_page) || $per_page < 1) { $per_page = $screen->get_option('per_page', 'default'); } $per_page = (int) $per_page; $this->_column_headers = $this->get_column_info(); usort($data, array($this, 'usort_reorder_callback')); /* * Let's figure out what page the user is currently * looking at. */ $current_page = $this->get_pagenum(); /* * Let's check how many items are in our data array. */ $total_items = count($data); /* * The WP_List_Table class does not handle pagination for us, so we need * to ensure that the data is trimmed to only the current page. */ $data = array_slice($data, ($current_page - 1) * $per_page, $per_page); /* * Now we can add our *sorted* data to the items property, where * it can be used by the rest of the class. */ $this->items = $data; /* * We also have to register our pagination options & calculations. */ $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil($total_items / $per_page))); }
/** * Prepares the items to later display in the table. * Should run before any headers are sent. */ public function prepare_items() { global $status, $snippets, $totals, $s; wp_reset_vars(array('orderby', 'order', 's')); $screen = get_current_screen(); $user = get_current_user_id(); /* First, lets process the bulk actions */ $this->process_bulk_actions(); /* Initialize the $snippets array */ $snippets = array_fill_keys($this->statuses, array()); /* Fetch all snippets */ $snippets['all'] = get_snippets(array(), $this->is_network); $snippets['all'] = apply_filters('code_snippets/list_table/get_snippets', $snippets['all']); /* Fetch shared network snippets */ $this->fetch_shared_network_snippets(); /* Redirect POST'ed tag filter to GET */ if (isset($_POST['tag'])) { $location = empty($_POST['tag']) ? remove_query_arg('tag') : add_query_arg('tag', $_POST['tag']); wp_redirect(esc_url_raw($location)); exit; } /* Add scope tags */ if (code_snippets_get_setting('general', 'snippet_scope_enabled')) { foreach ($snippets['all'] as $snippet) { if (0 != $snippet->scope) { $snippet->tags = array_merge($snippet->tags, array($snippet->scope_name)); } } } /* Filter snippets by tag */ if (!empty($_GET['tag'])) { $snippets['all'] = array_filter($snippets['all'], array($this, 'tags_filter_callback')); } /* Filter snippets based on search query */ if ($s) { $snippets['all'] = array_filter($snippets['all'], array($this, 'search_callback')); } /* Clear recently activated snippets older than a week */ $recently_activated = $this->is_network ? get_site_option('recently_activated_snippets', array()) : get_option('recently_activated_snippets', array()); foreach ($recently_activated as $key => $time) { if ($time + WEEK_IN_SECONDS < time()) { unset($recently_activated[$key]); } } $this->is_network ? update_site_option('recently_activated_snippets', $recently_activated) : update_option('recently_activated_snippets', $recently_activated); /* Filter snippets into individual sections */ foreach ($snippets['all'] as $snippet) { if ($snippet->active) { $snippets['active'][] = $snippet; } else { $snippets['inactive'][] = $snippet; /* Was the snippet recently activated? */ if (isset($recently_activated[$snippet->id])) { $snippets['recently_activated'][] = $snippet; } } } /* Count the totals for each section */ $totals = array(); foreach ($snippets as $type => $list) { $totals[$type] = count($list); } /* If the current status is empty, default tp all */ if (empty($snippets[$status])) { $status = 'all'; } /* Get the current data */ $data = $snippets[$status]; /* Decide how many records per page to show by getting the user's setting in the Screen Options panel */ $sort_by = $screen->get_option('per_page', 'option'); $per_page = get_user_meta($user, $sort_by, true); if (empty($per_page) || $per_page < 1) { $per_page = $screen->get_option('per_page', 'default'); } $per_page = (int) $per_page; $this->_column_headers = $this->get_column_info(); usort($data, array($this, 'usort_reorder_callback')); /* Determine what page the user is currently looking at */ $current_page = $this->get_pagenum(); /* Check how many items are in the data array */ $total_items = count($data); /* The WP_List_Table class does not handle pagination for us, so we need to ensure that the data is trimmed to only the current page. */ $data = array_slice($data, ($current_page - 1) * $per_page, $per_page); /* Now we can add our *sorted* data to the items property, where it can be used by the rest of the class. */ $this->items = $data; /* We register our pagination options and calculations */ $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil($total_items / $per_page))); }