permalink-manager-debug.php
1 year ago
permalink-manager-permastructs.php
1 year ago
permalink-manager-settings.php
1 year ago
permalink-manager-tools.php
1 year ago
permalink-manager-ui-elements.php
1 year ago
permalink-manager-uri-editor-post.php
1 year ago
permalink-manager-uri-editor.php
1 year ago
permalink-manager-uri-editor-post.php
339 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Use WP_List_Table to display the "Bulk URI Editor" for post items |
| 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 $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 == 'all' ) ? "'" . implode( "', '", $permalink_manager_options['screen-options']['post_types'] ) . "'" : "'{$active_subsection}'"; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Get the HTML output with the whole WP_List_Table |
| 24 | * |
| 25 | * @return string |
| 26 | */ |
| 27 | public function display_admin_section() { |
| 28 | $output = "<form id=\"permalinks-post-types-table\" class=\"slugs-table\" method=\"post\">"; |
| 29 | $output .= wp_nonce_field( 'permalink-manager', 'uri_editor' ); |
| 30 | $output .= Permalink_Manager_UI_Elements::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 | /** |
| 47 | * Return an array of classes to be used in the HTML table |
| 48 | * |
| 49 | * @return array |
| 50 | */ |
| 51 | function get_table_classes() { |
| 52 | return array( 'widefat', 'striped', $this->_args['plural'] ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Add columns to the table |
| 57 | * |
| 58 | * @return array |
| 59 | */ |
| 60 | public function get_columns() { |
| 61 | return apply_filters( 'permalink_manager_uri_editor_columns', array( |
| 62 | 'item_title' => __( 'Post title', 'permalink-manager' ), |
| 63 | 'item_uri' => __( 'Custom permalink', 'permalink-manager' ) |
| 64 | ) ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Define sortable columns |
| 69 | * |
| 70 | * @return array |
| 71 | */ |
| 72 | public function get_sortable_columns() { |
| 73 | return array( |
| 74 | 'item_title' => array( 'post_title', false ) |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Data inside the columns |
| 80 | * |
| 81 | * @param array $item |
| 82 | * @param string $column_name |
| 83 | * |
| 84 | * @return string |
| 85 | */ |
| 86 | public function column_default( $item, $column_name ) { |
| 87 | global $permalink_manager_options; |
| 88 | |
| 89 | if ( Permalink_Manager_Helper_Functions::is_front_page( $item['ID'] ) ) { |
| 90 | $uri = ''; |
| 91 | $permalink = Permalink_Manager_Helper_Functions::get_permalink_base( $item['ID'] ); |
| 92 | $is_front_page = true; |
| 93 | } else { |
| 94 | $is_draft = ( $item["post_status"] == 'draft' ) ? true : false; |
| 95 | $uri = Permalink_Manager_URI_Functions_Post::get_post_uri( $item['ID'], true, $is_draft ); |
| 96 | $uri = ( ! empty( $permalink_manager_options['general']['decode_uris'] ) ) ? urldecode( $uri ) : $uri; |
| 97 | $permalink = get_permalink( $item['ID'] ); |
| 98 | $is_front_page = false; |
| 99 | } |
| 100 | |
| 101 | $field_args_base = array( 'type' => 'text', 'value' => $uri, 'without_label' => true, 'input_class' => 'custom_uri', 'extra_atts' => "data-element-id=\"{$item['ID']}\"" ); |
| 102 | $post_title = sanitize_text_field( $item['post_title'] ); |
| 103 | |
| 104 | $post_statuses_array = get_post_statuses(); |
| 105 | $post_statuses_array['inherit'] = __( 'Inherit (Attachment)', 'permalink-manager' ); |
| 106 | |
| 107 | $output = apply_filters( 'permalink_manager_uri_editor_column_content', '', $column_name, get_post( $item['ID'] ) ); |
| 108 | if ( ! empty( $output ) ) { |
| 109 | return $output; |
| 110 | } |
| 111 | |
| 112 | switch ( $column_name ) { |
| 113 | case 'item_uri': |
| 114 | // Get auto-update settings |
| 115 | $auto_update_val = get_post_meta( $item['ID'], "auto_update_uri", true ); |
| 116 | $auto_update_uri = ( ! empty( $auto_update_val ) ) ? $auto_update_val : $permalink_manager_options["general"]["auto_update_uris"]; |
| 117 | |
| 118 | if ( $is_front_page ) { |
| 119 | $field_args_base['disabled'] = true; |
| 120 | $field_args_base['append_content'] = sprintf( '<p class="small uri_locked">%s %s</p>', '<span class="dashicons dashicons-lock"></span>', __( 'URI Editor is disabled because a custom permalink cannot be set for a front page.', 'permalink-manager' ) ); |
| 121 | } else if ( Permalink_Manager_Helper_Functions::is_draft_excluded( (int) $item['ID'] ) ) { |
| 122 | $field_args_base['disabled'] = true; |
| 123 | $field_args_base['append_content'] = sprintf( '<p class="small uri_locked">%s %s</p>', '<span class="dashicons dashicons-lock"></span>', __( 'URI Editor disabled due to "Exclude drafts & pending posts" setting and the post status.', 'permalink-manager' ) ); |
| 124 | } else if ( $auto_update_uri == 1 ) { |
| 125 | $field_args_base['readonly'] = true; |
| 126 | $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' ) ); |
| 127 | } else if ( $auto_update_uri == 2 ) { |
| 128 | $field_args_base['disabled'] = true; |
| 129 | $field_args_base['append_content'] = sprintf( '<p class="small uri_locked">%s %s</p>', '<span class="dashicons dashicons-lock"></span>', __( 'URI Editor disabled due to "Permalink update" setting.', 'permalink-manager' ) ); |
| 130 | } |
| 131 | |
| 132 | $output = '<div class="custom_uri_container">'; |
| 133 | $output .= Permalink_Manager_UI_Elements::generate_option_field( "uri[{$item['ID']}]", $field_args_base ); |
| 134 | $output .= "<span class=\"duplicated_uri_alert\"></span>"; |
| 135 | $output .= sprintf( "<a class=\"small post_permalink\" href=\"%s\" target=\"_blank\"><span class=\"dashicons dashicons-admin-links\"></span> %s</a>", $permalink, urldecode( $permalink ) ); |
| 136 | $output .= '</div>'; |
| 137 | |
| 138 | return $output; |
| 139 | |
| 140 | case 'item_title': |
| 141 | $output = $post_title; |
| 142 | $output .= '<div class="extra-info small">'; |
| 143 | $output .= sprintf( "<span><strong>%s:</strong> %s</span>", __( "Slug", "permalink-manager" ), urldecode( $item['post_name'] ) ); |
| 144 | $output .= sprintf( " | <span><strong>%s:</strong> {$post_statuses_array[$item["post_status"]]}</span>", __( "Post status", "permalink-manager" ) ); |
| 145 | $output .= apply_filters( 'permalink_manager_uri_editor_extra_info', '', $column_name, get_post( $item['ID'] ) ); |
| 146 | $output .= '</div>'; |
| 147 | |
| 148 | $output .= '<div class="row-actions">'; |
| 149 | $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' ) ); |
| 150 | $output .= '<span class="view"><a target="_blank" href="' . $permalink . '" title="' . __( 'View', 'permalink-manager' ) . ' ' . $post_title . '" rel="permalink">' . __( 'View', 'permalink-manager' ) . '</a> | </span>'; |
| 151 | $output .= '<span class="id">#' . $item['ID'] . '</span>'; |
| 152 | $output .= '</div>'; |
| 153 | |
| 154 | return $output; |
| 155 | |
| 156 | default: |
| 157 | return $item[ $column_name ]; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * The button that allows to save updated slugs |
| 163 | */ |
| 164 | function extra_tablenav( $which ) { |
| 165 | global $wpdb, $active_section, $active_subsection; |
| 166 | |
| 167 | if ( $which == "top" ) { |
| 168 | $button_text = __( 'Save all the permalinks below', 'permalink-manager' ); |
| 169 | $button_name = 'update_all_slugs[top]'; |
| 170 | } else { |
| 171 | $button_text = __( 'Save all the permalinks above', 'permalink-manager' ); |
| 172 | $button_name = 'update_all_slugs[bottom]'; |
| 173 | } |
| 174 | |
| 175 | $html = "<div class=\"alignleft actions\">"; |
| 176 | $html .= get_submit_button( $button_text, 'primary alignleft', $button_name, false, array( 'id' => 'doaction', 'value' => 'update_all_slugs' ) ); |
| 177 | |
| 178 | if ( $which == "top" ) { |
| 179 | $html .= '<div class="alignright">'; |
| 180 | $html .= $this->search_box( __( 'Search', 'permalink-manager' ), 'search-input' ); |
| 181 | $html .= '</div>'; |
| 182 | |
| 183 | // Filter by date |
| 184 | $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 ); |
| 185 | if ( $months ) { |
| 186 | $month_key = 'month'; |
| 187 | $screen = get_current_screen(); |
| 188 | $current_url = add_query_arg( array( |
| 189 | 'page' => PERMALINK_MANAGER_PLUGIN_SLUG, |
| 190 | 'section' => $active_section, |
| 191 | 'subsection' => $active_subsection |
| 192 | ), admin_url( $screen->parent_file ) ); |
| 193 | |
| 194 | $html .= sprintf( "<div id=\"months-filter\" class=\"alignright hide-if-no-js\" data-filter-url=\"%s\">", $current_url ); |
| 195 | $html .= sprintf( "<select id=\"months-filter-select\" name=\"%s\">", $month_key ); |
| 196 | $html .= sprintf( "<option value=\"\">%s</option>", __( "All dates", "permalink-manager" ) ); |
| 197 | foreach ( $months as $month ) { |
| 198 | $month_raw = sprintf( "%s-%s", $month['y'], $month['m'] ); |
| 199 | $month_human_name = date_i18n( "F Y", strtotime( $month_raw ) ); |
| 200 | |
| 201 | $selected = ( ! empty( $_REQUEST[ $month_key ] ) ) ? selected( $_REQUEST[ $month_key ], $month_raw, false ) : ""; |
| 202 | $html .= sprintf( "<option value=\"%s\" %s>%s</option>", $month_raw, $selected, $month_human_name ); |
| 203 | } |
| 204 | $html .= "</select>"; |
| 205 | $html .= get_submit_button( __( "Filter", "permalink-manager" ), 'button', false, false, array( 'id' => 'months-filter-button', 'name' => 'months-filter-button' ) ); |
| 206 | $html .= "</div>"; |
| 207 | } |
| 208 | } |
| 209 | $html .= "</div>"; |
| 210 | |
| 211 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 212 | echo $html; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Display the search input field |
| 217 | * |
| 218 | * @return string |
| 219 | */ |
| 220 | public function search_box( $text = '', $input_id = '' ) { |
| 221 | $search_query = ( ! empty( $_REQUEST['s'] ) ) ? esc_attr( $_REQUEST['s'] ) : ""; |
| 222 | |
| 223 | $output = "<p class=\"search-box\">"; |
| 224 | $output .= "<label class=\"screen-reader-text\" for=\"{$input_id}\">{$text}:</label>"; |
| 225 | $output .= Permalink_Manager_UI_Elements::generate_option_field( 's', array( 'value' => $search_query, 'type' => 'search' ) ); |
| 226 | $output .= get_submit_button( $text, 'button', false, false, array( 'id' => 'search-submit', 'name' => 'search-submit' ) ); |
| 227 | $output .= "</p>"; |
| 228 | |
| 229 | return $output; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Prepare the items for the table to process |
| 234 | */ |
| 235 | public function prepare_items() { |
| 236 | global $wpdb, $permalink_manager_options; |
| 237 | |
| 238 | $columns = $this->get_columns(); |
| 239 | $hidden = $this->get_hidden_columns(); |
| 240 | $sortable = $this->get_sortable_columns(); |
| 241 | $current_page = $this->get_pagenum(); |
| 242 | |
| 243 | // Get query variables |
| 244 | $per_page = $permalink_manager_options['screen-options']['per_page']; |
| 245 | |
| 246 | // SQL query parameters |
| 247 | $order = ( isset( $_REQUEST['order'] ) && in_array( $_REQUEST['order'], array( 'asc', 'desc' ) ) ) ? sanitize_sql_orderby( $_REQUEST['order'] ) : 'desc'; |
| 248 | $orderby = ( isset( $_REQUEST['orderby'] ) ) ? sanitize_sql_orderby( $_REQUEST['orderby'] ) : 'ID'; |
| 249 | $offset = ( $current_page - 1 ) * $per_page; |
| 250 | $search_query = ( ! empty( $_REQUEST['s'] ) ) ? esc_sql( $_REQUEST['s'] ) : ""; |
| 251 | |
| 252 | // Extra filters |
| 253 | $extra_filters = $attachment_support = ''; |
| 254 | if ( ! empty( $_GET['month'] ) ) { |
| 255 | $month = date( "n", strtotime( $_GET['month'] ) ); |
| 256 | $year = date( "Y", strtotime( $_GET['month'] ) ); |
| 257 | |
| 258 | $extra_filters .= "AND month(post_date) = {$month} AND year(post_date) = {$year}"; |
| 259 | } |
| 260 | |
| 261 | // Support for attachments |
| 262 | if ( strpos( $this->displayed_post_types, 'attachment' ) !== false ) { |
| 263 | $attachment_support = " OR (post_type = 'attachment')"; |
| 264 | } |
| 265 | |
| 266 | // Grab posts from database |
| 267 | $sql_parts['start'] = "SELECT * FROM {$wpdb->posts} "; |
| 268 | if ( $search_query ) { |
| 269 | $sql_parts['where'] = "WHERE (LOWER(post_title) LIKE LOWER('%{$search_query}%') "; |
| 270 | |
| 271 | // Search in array with custom URIs |
| 272 | $found = Permalink_Manager_URI_Functions::find_uri( $search_query, false, 'posts' ); |
| 273 | if ( $found ) { |
| 274 | $sql_parts['where'] .= sprintf( "OR ID IN (%s)", implode( ',', $found ) ); |
| 275 | } |
| 276 | $sql_parts['where'] .= " ) AND ((post_status IN ($this->displayed_post_statuses) AND post_type IN ($this->displayed_post_types)) {$attachment_support}) {$extra_filters} "; |
| 277 | } else { |
| 278 | $sql_parts['where'] = "WHERE ((post_status IN ($this->displayed_post_statuses) AND post_type IN ($this->displayed_post_types)) {$attachment_support}) {$extra_filters} "; |
| 279 | } |
| 280 | |
| 281 | // Do not display excluded posts in Bulk URI Editor |
| 282 | $excluded_posts = Permalink_Manager_Helper_Functions::get_excluded_post_ids(); |
| 283 | if ( ! empty( $excluded_posts ) && is_array( $excluded_posts ) ) { |
| 284 | $sql_parts['where'] .= sprintf( "AND ID NOT IN ('%s') ", implode( "', '", $excluded_posts ) ); |
| 285 | } |
| 286 | |
| 287 | $sql_parts['end'] = "ORDER BY {$orderby} {$order}"; |
| 288 | |
| 289 | // Prepare the SQL query |
| 290 | $sql_query = implode( "", $sql_parts ); |
| 291 | |
| 292 | // Count items |
| 293 | $count_query = str_replace( 'SELECT *', 'SELECT COUNT(*)', $sql_query ); |
| 294 | $total_items = $wpdb->get_var( $count_query ); |
| 295 | |
| 296 | // Pagination support |
| 297 | $sql_query .= sprintf( " LIMIT %d, %d", $offset, $per_page ); |
| 298 | |
| 299 | // Get items |
| 300 | $sql_query = apply_filters( 'permalink_manager_filter_uri_editor_query', $sql_query, $this, $sql_parts, $is_taxonomy = false ); |
| 301 | $all_items = $wpdb->get_results( $sql_query, ARRAY_A ); |
| 302 | |
| 303 | $this->set_pagination_args( array( |
| 304 | 'total_items' => $total_items, |
| 305 | 'per_page' => $per_page |
| 306 | ) ); |
| 307 | |
| 308 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 309 | $this->items = $all_items; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Define hidden columns |
| 314 | * |
| 315 | * @return array |
| 316 | */ |
| 317 | public function get_hidden_columns() { |
| 318 | return array(); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Sort the data |
| 323 | * |
| 324 | * @param mixed $a |
| 325 | * @param mixed $b |
| 326 | * |
| 327 | * @return int |
| 328 | */ |
| 329 | private function sort_data( $a, $b ) { |
| 330 | // Set defaults |
| 331 | $orderby = ( ! empty( $_GET['orderby'] ) ) ? sanitize_sql_orderby( $_GET['orderby'] ) : 'post_title'; |
| 332 | $order = ( ! empty( $_GET['order'] ) ) ? sanitize_sql_orderby( $_GET['order'] ) : 'asc'; |
| 333 | $result = strnatcasecmp( $a[ $orderby ], $b[ $orderby ] ); |
| 334 | |
| 335 | return ( $order === 'asc' ) ? $result : - $result; |
| 336 | } |
| 337 | |
| 338 | } |
| 339 |