permalink-manager-debug.php
5 years ago
permalink-manager-permastructs.php
5 years ago
permalink-manager-settings.php
5 years ago
permalink-manager-tools.php
5 years ago
permalink-manager-uri-editor-post.php
5 years ago
permalink-manager-uri-editor.php
5 years ago
permalink-manager-uri-editor-post.php
287 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Extend WP_List_Table with two subclasses for post types and taxonomies |
| 5 | */ |
| 6 | class Permalink_Manager_URI_Editor_Post extends WP_List_Table { |
| 7 | |
| 8 | public $displayed_post_types, $displayed_post_statuses; |
| 9 | |
| 10 | public function __construct() { |
| 11 | global $status, $page, $permalink_manager_options, $active_subsection; |
| 12 | |
| 13 | parent::__construct(array( |
| 14 | 'singular' => 'slug', |
| 15 | 'plural' => 'slugs' |
| 16 | )); |
| 17 | |
| 18 | $this->displayed_post_statuses = (isset($permalink_manager_options['screen-options']['post_statuses'])) ? "'" . implode("', '", $permalink_manager_options['screen-options']['post_statuses']) . "'" : "'no-post-status'"; |
| 19 | $this->displayed_post_types = ($active_subsection && $active_subsection == 'all') ? "'" . implode("', '", $permalink_manager_options['screen-options']['post_types']) . "'" : "'{$active_subsection}'"; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Get the HTML output with the WP_List_Table |
| 24 | */ |
| 25 | public function display_admin_section() { |
| 26 | global $wpdb; |
| 27 | |
| 28 | $output = "<form id=\"permalinks-post-types-table\" class=\"slugs-table\" method=\"post\">"; |
| 29 | $output .= wp_nonce_field('permalink-manager', 'uri_editor', true, true); |
| 30 | $output .= Permalink_Manager_Admin_Functions::generate_option_field('pm_session_id', array('value' => uniqid(), 'type' => 'hidden')); |
| 31 | |
| 32 | // Bypass |
| 33 | ob_start(); |
| 34 | |
| 35 | $this->prepare_items(); |
| 36 | $this->display(); |
| 37 | $output .= ob_get_contents(); |
| 38 | |
| 39 | ob_end_clean(); |
| 40 | |
| 41 | $output .= "</form>"; |
| 42 | |
| 43 | return $output; |
| 44 | } |
| 45 | |
| 46 | function get_table_classes() { |
| 47 | return array( 'widefat', 'striped', $this->_args['plural'] ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Override the parent columns method. Defines the columns to use in your listing table |
| 52 | */ |
| 53 | public function get_columns() { |
| 54 | return apply_filters('permalink_manager_uri_editor_columns', array( |
| 55 | 'item_title' => __('Post title', 'permalink-manager'), |
| 56 | 'item_uri' => __('Full URI & Permalink', 'permalink-manager') |
| 57 | )); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Hidden columns |
| 62 | */ |
| 63 | public function get_hidden_columns() { |
| 64 | return array(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Sortable columns |
| 69 | */ |
| 70 | public function get_sortable_columns() { |
| 71 | return array( |
| 72 | 'item_title' => array('post_title', false) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Data inside the columns |
| 78 | */ |
| 79 | public function column_default($item, $column_name) { |
| 80 | global $permalink_manager_options; |
| 81 | |
| 82 | $uri = Permalink_Manager_URI_Functions_Post::get_post_uri($item['ID'], true); |
| 83 | $uri = (!empty($permalink_manager_options['general']['decode_uris'])) ? urldecode($uri) : $uri; |
| 84 | |
| 85 | $field_args_base = array('type' => 'text', 'value' => $uri, 'without_label' => true, 'input_class' => 'custom_uri', 'extra_atts' => "data-element-id=\"{$item['ID']}\""); |
| 86 | $permalink = get_permalink($item['ID']); |
| 87 | |
| 88 | $post_statuses_array = get_post_statuses(); |
| 89 | $post_statuses_array['inherit'] = __('Inherit (Attachment)', 'permalink-manager'); |
| 90 | |
| 91 | $output = apply_filters('permalink_manager_uri_editor_column_content', '', $column_name, get_post($item['ID'])); |
| 92 | if(!empty($output)) { return $output; } |
| 93 | |
| 94 | switch( $column_name ) { |
| 95 | case 'item_uri': |
| 96 | // Get auto-update settings |
| 97 | $auto_update_val = get_post_meta($item['ID'], "auto_update_uri", true); |
| 98 | $auto_update_uri = (!empty($auto_update_val)) ? $auto_update_val : $permalink_manager_options["general"]["auto_update_uris"]; |
| 99 | if($auto_update_uri) { |
| 100 | $field_args_base['readonly'] = true; |
| 101 | $field_args_base['append_content'] = sprintf('<p class="small uri_locked">%s %s</p>', '<span class="dashicons dashicons-lock"></span>', __('The above permalink will be automatically updated and is locked for editing.', 'permalink-manager')); |
| 102 | } |
| 103 | |
| 104 | $output = '<div class="custom_uri_container">'; |
| 105 | $output .= Permalink_Manager_Admin_Functions::generate_option_field("uri[{$item['ID']}]", $field_args_base); |
| 106 | $output .= "<span class=\"duplicated_uri_alert\"></span>"; |
| 107 | $output .= sprintf("<a class=\"small post_permalink\" href=\"%s\" target=\"_blank\"><span class=\"dashicons dashicons-admin-links\"></span> %s</a>", $permalink, urldecode($permalink)); |
| 108 | $output .= '</div>'; |
| 109 | return $output; |
| 110 | |
| 111 | case 'item_title': |
| 112 | $output = $item[ 'post_title' ]; |
| 113 | $output .= '<div class="extra-info small">'; |
| 114 | $output .= sprintf("<span><strong>%s:</strong> %s</span>", __("Slug", "permalink-manager"), urldecode($item['post_name'])); |
| 115 | $output .= sprintf(" | <span><strong>%s:</strong> {$post_statuses_array[$item["post_status"]]}</span>", __("Post status", "permalink-manager")); |
| 116 | $output .= apply_filters('permalink_manager_uri_editor_extra_info', '', $column_name, get_post($item['ID'])); |
| 117 | $output .= '</div>'; |
| 118 | |
| 119 | $output .= '<div class="row-actions">'; |
| 120 | $output .= sprintf("<span class=\"edit\"><a href=\"%s/wp-admin/post.php?post={$item['ID']}&action=edit\" title=\"%s\">%s</a> | </span>", get_option('home'), __('Edit', 'permalink-manager'), __('Edit', 'permalink-manager')); |
| 121 | $output .= '<span class="view"><a target="_blank" href="' . $permalink . '" title="' . __('View', 'permalink-manager') . ' ' . $item[ 'post_title' ] . '" rel="permalink">' . __('View', 'permalink-manager') . '</a> | </span>'; |
| 122 | $output .= '<span class="id">#' . $item[ 'ID' ] . '</span>'; |
| 123 | $output .= '</div>'; |
| 124 | return $output; |
| 125 | |
| 126 | default: |
| 127 | return $item[$column_name]; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Sort the data |
| 133 | */ |
| 134 | private function sort_data($a, $b) { |
| 135 | // Set defaults |
| 136 | $orderby = (!empty($_GET['orderby'])) ? $_GET['orderby'] : 'post_title'; |
| 137 | $order = (!empty($_GET['order'])) ? $_GET['order'] : 'asc'; |
| 138 | $result = strnatcasecmp( $a[$orderby], $b[$orderby] ); |
| 139 | |
| 140 | return ($order === 'asc') ? $result : -$result; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * The button that allows to save updated slugs |
| 145 | */ |
| 146 | function extra_tablenav($which) { |
| 147 | global $wpdb, $active_section, $active_subsection; |
| 148 | |
| 149 | $button_top = __( 'Save all the URIs below', 'permalink-manager' ); |
| 150 | $button_bottom = __( 'Save all the URIs above', 'permalink-manager' ); |
| 151 | |
| 152 | $html = "<div class=\"alignleft actions\">"; |
| 153 | $html .= get_submit_button( ${"button_$which"}, 'primary alignleft', "update_all_slugs[{$which}]", false, array( 'id' => 'doaction', 'value' => 'update_all_slugs' ) ); |
| 154 | |
| 155 | if ($which == "top") { |
| 156 | // Searchbox |
| 157 | $html .= '<div class="alignright">'; |
| 158 | $html .= $this->search_box(__('Search', 'permalink-manager'), 'search-input'); |
| 159 | $html .= '</div>'; |
| 160 | |
| 161 | // Filter by date |
| 162 | $months = $wpdb->get_results("SELECT DISTINCT month(post_date) AS m, year(post_date) AS y FROM {$wpdb->posts} WHERE post_status IN ($this->displayed_post_statuses) AND post_type IN ($this->displayed_post_types) ORDER BY post_date DESC", ARRAY_A); |
| 163 | if($months) { |
| 164 | $month_key = 'month'; |
| 165 | $screen = get_current_screen(); |
| 166 | $current_url = add_query_arg(array( |
| 167 | 'page' => PERMALINK_MANAGER_PLUGIN_SLUG, |
| 168 | 'section' => $active_section, |
| 169 | 'subsection' => $active_subsection |
| 170 | ), admin_url($screen->parent_file)); |
| 171 | |
| 172 | $html .= "<div id=\"months-filter\" class=\"alignright hide-if-no-js\" data-filter-url=\"{$current_url}\">"; |
| 173 | $html .= "<select id=\"months-filter-select\" name=\"{$month_key}\">"; |
| 174 | $html .= sprintf("<option value=\"\">%s</option>", __("All dates", "permalink-manager")); |
| 175 | foreach($months as $month) { |
| 176 | $month_raw = "{$month['y']}-{$month['m']}"; |
| 177 | $month_human_name = date_i18n("F Y", strtotime($month_raw)); |
| 178 | |
| 179 | $selected = (!empty($_REQUEST[$month_key])) ? selected($_REQUEST[$month_key], $month_raw, false) : ""; |
| 180 | $html .= "<option value=\"{$month_raw}\" {$selected}>{$month_human_name}</option>"; |
| 181 | } |
| 182 | $html .= "</select>"; |
| 183 | $html .= get_submit_button(__("Filter", "permalink-manager"), 'button', false, false, array('id' => 'months-filter-button', 'name' => 'months-filter-button')); |
| 184 | $html .= "</div>"; |
| 185 | } |
| 186 | } |
| 187 | $html .= "</div>"; |
| 188 | |
| 189 | echo $html; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Search box |
| 194 | */ |
| 195 | public function search_box($text = '', $input_id = '') { |
| 196 | $search_query = (!empty($_REQUEST['s'])) ? esc_attr(wp_unslash($_REQUEST['s'])) : ""; |
| 197 | |
| 198 | $output = "<p class=\"search-box\">"; |
| 199 | $output .= "<label class=\"screen-reader-text\" for=\"{$input_id}\">{$text}:</label>"; |
| 200 | $output .= Permalink_Manager_Admin_Functions::generate_option_field('s', array('value' => $search_query, 'type' => 'search')); |
| 201 | $output .= get_submit_button($text, 'button', false, false, array('id' => 'search-submit', 'name' => 'search-submit')); |
| 202 | $output .= "</p>"; |
| 203 | |
| 204 | return $output; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Prepare the items for the table to process |
| 209 | */ |
| 210 | public function prepare_items() { |
| 211 | global $wpdb, $permalink_manager_options; |
| 212 | |
| 213 | $columns = $this->get_columns(); |
| 214 | $hidden = $this->get_hidden_columns(); |
| 215 | $sortable = $this->get_sortable_columns(); |
| 216 | $current_page = $this->get_pagenum(); |
| 217 | |
| 218 | // Get query variables |
| 219 | $per_page = $permalink_manager_options['screen-options']['per_page']; |
| 220 | |
| 221 | // SQL query parameters |
| 222 | $order = (isset($_REQUEST['order']) && in_array($_REQUEST['order'], array('asc', 'desc'))) ? $_REQUEST['order'] : 'desc'; |
| 223 | $orderby = (isset($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'ID'; |
| 224 | $offset = ($current_page - 1) * $per_page; |
| 225 | $search_query = (!empty($_REQUEST['s'])) ? esc_sql($_REQUEST['s']) : ""; |
| 226 | |
| 227 | // Extra filters |
| 228 | $extra_filters = $attachment_support = ''; |
| 229 | if(!empty($_GET['month'])) { |
| 230 | $month = date("n", strtotime($_GET['month'])); |
| 231 | $year = date("Y", strtotime($_GET['month'])); |
| 232 | |
| 233 | $extra_filters .= "AND month(post_date) = {$month} AND year(post_date) = {$year}"; |
| 234 | } |
| 235 | |
| 236 | // Support for attachments |
| 237 | if(strpos($this->displayed_post_types, 'attachment') !== false) { |
| 238 | $attachment_support = " OR (post_type = 'attachment')"; |
| 239 | } |
| 240 | |
| 241 | // Grab posts from database |
| 242 | $sql_parts['start'] = "SELECT * FROM {$wpdb->posts} "; |
| 243 | if($search_query) { |
| 244 | $sql_parts['where'] = "WHERE (LOWER(post_title) LIKE LOWER('%{$search_query}%') "; |
| 245 | |
| 246 | // Search in array with custom URIs |
| 247 | $found = Permalink_Manager_Helper_Functions::search_uri($search_query, 'posts'); |
| 248 | if($found) { |
| 249 | $sql_parts['where'] .= sprintf("OR ID IN (%s)", implode(',', (array) $found)); |
| 250 | } |
| 251 | $sql_parts['where'] .= " ) AND ((post_status IN ($this->displayed_post_statuses) AND post_type IN ($this->displayed_post_types)) {$attachment_support}) {$extra_filters} "; |
| 252 | } else { |
| 253 | $sql_parts['where'] = "WHERE ((post_status IN ($this->displayed_post_statuses) AND post_type IN ($this->displayed_post_types)) {$attachment_support}) {$extra_filters} "; |
| 254 | } |
| 255 | $sql_parts['end'] = "ORDER BY {$orderby} {$order}"; |
| 256 | |
| 257 | // Prepare the SQL query |
| 258 | $sql_query = implode("", $sql_parts); |
| 259 | |
| 260 | // Count items |
| 261 | $count_query = str_replace('SELECT *', 'SELECT COUNT(*)', $sql_query); |
| 262 | $total_items = $wpdb->get_var($count_query); |
| 263 | |
| 264 | // Pagination support |
| 265 | $sql_query .= sprintf(" LIMIT %d, %d", $offset, $per_page); |
| 266 | |
| 267 | // Get items |
| 268 | $sql_query = apply_filters('permalink_manager_filter_uri_editor_query', $sql_query, $this, $sql_parts, $is_taxonomy = false); |
| 269 | $all_items = $wpdb->get_results($sql_query, ARRAY_A); |
| 270 | |
| 271 | // Debug SQL query |
| 272 | if(isset($_REQUEST['debug_editor_sql'])) { |
| 273 | $debug_txt = "<textarea style=\"width:100%;height:300px\">{$sql_query} \n\nOffset: {$offset} \nPage: {$current_page}\nPer page: {$per_page} \nTotal: {$total_items}</textarea>"; |
| 274 | wp_die($debug_txt); |
| 275 | } |
| 276 | |
| 277 | $this->set_pagination_args( array( |
| 278 | 'total_items' => $total_items, |
| 279 | 'per_page' => $per_page |
| 280 | )); |
| 281 | |
| 282 | $this->_column_headers = array($columns, $hidden, $sortable); |
| 283 | $this->items = $all_items; |
| 284 | } |
| 285 | |
| 286 | } |
| 287 |