permalink-manager-actions.php
9 years ago
permalink-manager-base-editor.php
9 years ago
permalink-manager-editor.php
9 years ago
permalink-manager-helper-functions.php
9 years ago
permalink-manager-screen-options.php
9 years ago
permalink-manager-editor.php
180 lines
| 1 | <?php |
| 2 | if( ! class_exists( 'WP_List_Table' ) ) { |
| 3 | require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * Create a new table class that will extend the WP_List_Table |
| 8 | */ |
| 9 | class Permalink_Manager_Editor extends WP_List_Table { |
| 10 | public $screen_options_fields; |
| 11 | |
| 12 | function __construct() { |
| 13 | global $status, $page; |
| 14 | |
| 15 | parent::__construct(array( |
| 16 | 'singular' => 'slug', |
| 17 | 'plural' => 'slugs' |
| 18 | )); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Override the parent columns method. Defines the columns to use in your listing table |
| 23 | */ |
| 24 | public function get_columns() { |
| 25 | $columns = array( |
| 26 | //'cb' => '<input type="checkbox" />', //Render a checkbox instead of text |
| 27 | 'post_title' => __('Title', 'permalink-manager'), |
| 28 | 'post_name' => __('Post Name (Native Slug)', 'permalink-manager'), |
| 29 | //'post_date_gmt' => __('Date', 'permalink-manager'), |
| 30 | 'base_with_slug' => __('Base & Slug (URI)', 'permalink-manager'), |
| 31 | 'post_status' => __('Post Status', 'permalink-manager'), |
| 32 | 'post_type' => __('Post Type', 'permalink-manager') |
| 33 | ); |
| 34 | |
| 35 | return $columns; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Hidden columns |
| 40 | */ |
| 41 | public function get_hidden_columns() { |
| 42 | return array('post_date_gmt'); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Sortable columns |
| 47 | */ |
| 48 | public function get_sortable_columns() { |
| 49 | return array( |
| 50 | 'post_title' => array('post_title', false), |
| 51 | 'post_name' => array('post_name', false), |
| 52 | 'post_status' => array('post_status', false), |
| 53 | 'post_type' => array('post_type', false), |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Data inside the columns |
| 59 | */ |
| 60 | public function column_default( $item, $column_name ) { |
| 61 | |
| 62 | $base = Permalink_Manager_Helper_Functions::get_uri($item[ 'ID' ], false, false); |
| 63 | $field_args_base = array('type' => 'text', 'default' => $base, 'without_label' => true, 'input_class' => 'small'); |
| 64 | |
| 65 | switch( $column_name ) { |
| 66 | case 'post_type': |
| 67 | $post_type_labels = Permalink_Manager_Helper_Functions::get_post_types_array('full', $item['post_type']); |
| 68 | return "{$post_type_labels['label']}<br /><small>({$post_type_labels['name']})</small>"; |
| 69 | |
| 70 | case 'post_status': |
| 71 | $post_statuses_array = get_post_statuses(); |
| 72 | return "{$post_statuses_array[$item[ $column_name ]]}<br /><small>({$item[ $column_name ]})</small>"; |
| 73 | |
| 74 | case 'post_name': |
| 75 | return $item[ 'post_name' ]; |
| 76 | |
| 77 | case 'base_with_slug': |
| 78 | return ($item['post_status'] != 'publish') ? '-' : Permalink_Manager_Helper_Functions::generate_option_field("uri[{$item['ID']}]", $field_args_base); |
| 79 | |
| 80 | case 'post_title': |
| 81 | $edit_post = $item[ 'post_title' ]; |
| 82 | $edit_post .= '<small>' . Permalink_Manager_Helper_Functions::get_correct_permalink($item[ 'ID' ]) . '</small>'; |
| 83 | $edit_post .= '<div class="row-actions">'; |
| 84 | $edit_post .= '<span class="edit"><a target="_blank" href="' . home_url() . '/wp-admin/post.php?post=' . $item[ 'ID' ] . '&action=edit" title="' . __('Edit', 'permalink-manager') . '">' . __('Edit', 'permalink-manager') . '</a> | </span>'; |
| 85 | $edit_post .= '<span class="view"><a target="_blank" href="' . Permalink_Manager_Helper_Functions::get_correct_permalink($item[ 'ID' ]) . '" title="' . __('View', 'permalink-manager') . ' ' . $item[ 'post_title' ] . '" rel="permalink">' . __('View', 'permalink-manager') . '</a> | </span>'; |
| 86 | $edit_post .= '<span class="id">#' . $item[ 'ID' ] . '</span>'; |
| 87 | $edit_post .= '</div>'; |
| 88 | return $edit_post; |
| 89 | |
| 90 | default: |
| 91 | return $item[ $column_name ]; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Sort the data |
| 97 | */ |
| 98 | private function sort_data( $a, $b ) { |
| 99 | // Set defaults |
| 100 | $orderby = (!empty($_GET['orderby'])) ? $_GET['orderby'] : 'post_title'; |
| 101 | $order = (!empty($_GET['order'])) ? $_GET['order'] : 'asc'; |
| 102 | $result = strnatcasecmp( $a[$orderby], $b[$orderby] ); |
| 103 | |
| 104 | return ($order === 'asc') ? $result : -$result; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * The button that allows to save updated slugs |
| 109 | */ |
| 110 | function extra_tablenav( $which ) { |
| 111 | $button_top = __( 'Update all slugs below', 'permalink-manager' ); |
| 112 | $button_bottom = __( 'Update all slugs above', 'permalink-manager' ); |
| 113 | |
| 114 | echo '<div class="alignleft actions">'; |
| 115 | submit_button( ${"button_$which"}, 'primary', "update_all_slugs[{$which}]", false, array( 'id' => 'doaction', 'value' => 'update_all_slugs' ) ); |
| 116 | echo '</div>'; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Prepare the items for the table to process |
| 121 | */ |
| 122 | public function prepare_items() { |
| 123 | global $wpdb; |
| 124 | |
| 125 | $columns = $this->get_columns(); |
| 126 | $hidden = $this->get_hidden_columns(); |
| 127 | $sortable = $this->get_sortable_columns(); |
| 128 | $current_page = $this->get_pagenum(); |
| 129 | |
| 130 | // Load options and fields |
| 131 | $saved_options = get_option('permalink-manager'); |
| 132 | $saved_options = isset($saved_options['screen-options']) ? $saved_options['screen-options'] : array(); |
| 133 | $screen_options_fields = $this->screen_options_fields; |
| 134 | $per_page = isset($saved_options['per_page']) ? $saved_options['per_page'] : $screen_options_fields['per_page']['default']; |
| 135 | $post_types_array = isset($saved_options['post_types']) ? $saved_options['post_types'] : $screen_options_fields['post_types']['default']; |
| 136 | $post_types = "'" . implode("', '", $post_types_array) . "'"; |
| 137 | $post_statuses_array = isset($saved_options['post_statuses']) ? $saved_options['post_statuses'] : $screen_options_fields['post_statuses']['default']; |
| 138 | $post_statuses = "'" . implode("', '", $post_statuses_array) . "'"; |
| 139 | |
| 140 | // Will be used in pagination settings |
| 141 | $total_items = $wpdb->get_var("SELECT COUNT(id) FROM {$wpdb->posts} WHERE post_status IN ($post_statuses) AND post_type IN ($post_types)"); |
| 142 | |
| 143 | // SQL query parameters |
| 144 | $order = (isset($_REQUEST['order']) && in_array($_REQUEST['order'], array('asc', 'desc'))) ? $_REQUEST['order'] : 'desc'; |
| 145 | $orderby = (isset($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'ID'; |
| 146 | $offset = ($current_page - 1) * $per_page; |
| 147 | |
| 148 | // Grab posts from database |
| 149 | //$sql_query = "SELECT * FROM {$wpdb->posts} WHERE post_status IN ($post_statuses) AND post_type IN ($post_types) ORDER BY $orderby $order LIMIT $per_page OFFSET $offset"; |
| 150 | $sql_query = "SELECT * FROM {$wpdb->posts} WHERE post_status IN ($post_statuses) AND post_type IN ($post_types) ORDER BY $orderby $order"; |
| 151 | $all_data = $wpdb->get_results($sql_query, ARRAY_A); |
| 152 | |
| 153 | // Sort posts and count all posts |
| 154 | usort( $all_data, array( &$this, 'sort_data' ) ); |
| 155 | |
| 156 | $data = array_slice($all_data, $offset, $per_page); |
| 157 | |
| 158 | // Debug SQL query |
| 159 | $debug_txt = "<textarea style=\"width:100%;height:300px\">{$sql_query} \n\nOffset: {$offset} \nPage: {$current_page}\nPer page: {$per_page} \nTotal: {$total_items}</textarea>"; |
| 160 | if(isset($_REQUEST['debug_editor_sql'])) { wp_die($debug_txt); } |
| 161 | |
| 162 | $this->set_pagination_args( array( |
| 163 | 'total_items' => $total_items, |
| 164 | 'per_page' => $per_page |
| 165 | )); |
| 166 | |
| 167 | $this->_column_headers = array($columns, $hidden, $sortable); |
| 168 | $this->items = $data; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * This variable is assigned in permalink-manager.php before prepare_items() function is triggered, see permalinks_table_html() function |
| 173 | */ |
| 174 | public function set_screen_option_fields($fields) { |
| 175 | $this->screen_options_fields = $fields; |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | ?> |
| 180 |