'slug', 'plural' => 'slugs' ) ); $this->displayed_post_statuses = ( isset( $permalink_manager_options['screen-options']['post_statuses'] ) ) ? Permalink_Manager_Helper_Functions::prepare_array_for_sql_in( $permalink_manager_options['screen-options']['post_statuses'] ) : "'no-post-status'"; $this->displayed_post_types = ( $active_subsection == 'all' ) ? Permalink_Manager_Helper_Functions::prepare_array_for_sql_in( $permalink_manager_options['screen-options']['post_types'] ) : "'{$active_subsection}'"; } /** * Get the HTML output with the whole WP_List_Table * * @return string */ public function display_admin_section() { $output = "
"; return $output; } /** * Return an array of classes to be used in the HTML table * * @return array */ function get_table_classes() { return array( 'widefat', 'striped', $this->_args['plural'] ); } /** * Add columns to the table * * @return array */ public function get_columns() { return apply_filters( 'permalink_manager_uri_editor_columns', array( 'item_title' => __( 'Post title', 'permalink-manager' ), 'item_uri' => __( 'Custom permalink', 'permalink-manager' ) ) ); } /** * Define sortable columns * * @return array */ public function get_sortable_columns() { return array( 'item_title' => array( 'post_title', false ) ); } /** * Data inside the columns * * @param array $item * @param string $column_name * * @return string */ public function column_default( $item, $column_name ) { global $permalink_manager_options; if ( Permalink_Manager_Helper_Functions::is_front_page( $item['ID'] ) ) { $uri = ''; $permalink = Permalink_Manager_Permastructure_Functions::get_permalink_base( $item['ID'] ); $is_front_page = true; } else { $is_draft = ( $item["post_status"] == 'draft' ) ? true : false; $uri = Permalink_Manager_URI_Functions_Post::get_post_uri( $item['ID'], true, $is_draft ); $uri = ( ! empty( $permalink_manager_options['general']['decode_uris'] ) ) ? urldecode( $uri ) : $uri; $permalink = get_permalink( $item['ID'] ); $is_front_page = false; } $field_args_base = array( 'type' => 'text', 'value' => $uri, 'without_label' => true, 'input_class' => 'custom_uri', 'extra_atts' => "data-element-id=\"{$item['ID']}\"" ); $post_title = sanitize_text_field( $item['post_title'] ); $post_statuses_array = Permalink_Manager_Helper_Functions::get_post_statuses(); $post_statuses_array['inherit'] = __( 'Inherit (Attachment)', 'permalink-manager' ); $output = apply_filters( 'permalink_manager_uri_editor_column_content', '', $column_name, get_post( $item['ID'] ) ); if ( ! empty( $output ) ) { return $output; } switch ( $column_name ) { case 'item_uri': // Get auto-update settings $auto_update_val = get_post_meta( $item['ID'], "auto_update_uri", true ); $auto_update_uri = ( ! empty( $auto_update_val ) ) ? $auto_update_val : $permalink_manager_options["general"]["auto_update_uris"]; if ( $is_front_page ) { $field_args_base['disabled'] = true; $field_args_base['append_content'] = sprintf( '%s %s
', '', __( 'URI Editor is disabled because a custom permalink cannot be set for a front page.', 'permalink-manager' ) ); } else if ( Permalink_Manager_Helper_Functions::is_draft_excluded( (int) $item['ID'] ) ) { $field_args_base['disabled'] = true; $field_args_base['append_content'] = sprintf( '%s %s
', '', __( 'URI Editor disabled due to "Exclude drafts & pending posts" setting and the post status.', 'permalink-manager' ) ); } else if ( $auto_update_uri == 1 ) { $field_args_base['readonly'] = true; $field_args_base['append_content'] = sprintf( '%s %s
', '', __( 'The above permalink will be automatically updated and is locked for editing.', 'permalink-manager' ) ); } else if ( $auto_update_uri == 2 ) { $field_args_base['disabled'] = true; $field_args_base['append_content'] = sprintf( '%s %s
', '', __( 'URI Editor disabled due to "Permalink update" setting.', 'permalink-manager' ) ); } $output = '"; $output .= ""; $output .= Permalink_Manager_UI_Elements::generate_option_field( 's', array( 'value' => $search_query, 'type' => 'search' ) ); $output .= get_submit_button( $text, 'button', false, false, array( 'id' => 'search-submit', 'name' => 'search-submit' ) ); $output .= "
"; return $output; } /** * Prepare the items for the table to process */ public function prepare_items() { global $wpdb; $columns = $this->get_columns(); $hidden = $this->get_hidden_columns(); $sortable = $this->get_sortable_columns(); $current_page = $this->get_pagenum(); // SQL query parameters $order = ( isset( $_REQUEST['order'] ) && in_array( $_REQUEST['order'], array( 'asc', 'desc' ) ) ) ? sanitize_sql_orderby( wp_unslash( $_REQUEST['order'] ) ) : 'desc'; $orderby = ( isset( $_REQUEST['orderby'] ) ) ? sanitize_sql_orderby( wp_unslash( $_REQUEST['orderby'] ) ) : 'ID'; $search_query = ( ! empty( $_REQUEST['s'] ) ) ? esc_sql( sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) ) : ""; // Extra filters $extra_filters = $attachment_support = ''; if ( ! empty( $_GET['month'] ) ) { $month = gmdate( "n", strtotime( sanitize_key( $_GET['month'] ) ) ); $year = gmdate( "Y", strtotime( sanitize_key( $_GET['month'] ) ) ); $extra_filters .= "AND month(post_date) = {$month} AND year(post_date) = {$year}"; } // Support for attachments if ( strpos( $this->displayed_post_types, 'attachment' ) !== false ) { $attachment_support = " OR (post_type = 'attachment')"; } // Grab posts from database $sql_parts['start'] = "SELECT * FROM {$wpdb->posts} AS p "; if ( $search_query ) { $sql_parts['where'] = "WHERE (LOWER(post_title) LIKE LOWER('%{$search_query}%') "; // Search in array with custom URIs $found = Permalink_Manager_URI_Functions::find_uri( $search_query, false, 'posts' ); if ( $found ) { $sql_parts['where'] .= sprintf( "OR ID IN (%s)", implode( ',', $found ) ); } $sql_parts['where'] .= " ) AND ((post_status IN ($this->displayed_post_statuses) AND post_type IN ($this->displayed_post_types)) {$attachment_support}) {$extra_filters} "; } else { $sql_parts['where'] = "WHERE ((post_status IN ($this->displayed_post_statuses) AND post_type IN ($this->displayed_post_types)) {$attachment_support}) {$extra_filters} "; } // Do not display excluded posts in Bulk URI Editor $excluded_posts = Permalink_Manager_Helper_Functions::get_excluded_post_ids(); if ( ! empty( $excluded_posts ) && is_array( $excluded_posts ) ) { $sql_parts['where'] .= sprintf( "AND ID NOT IN (%s) ", Permalink_Manager_Helper_Functions::prepare_array_for_sql_in( $excluded_posts ) ); } $sql_parts['end'] = "ORDER BY {$orderby} {$order}"; list( $all_items, $total_items, $per_page ) = Permalink_Manager_URI_Editor::prepare_sql_query( $sql_parts, $current_page, false ); $this->set_pagination_args( array( 'total_items' => $total_items, 'per_page' => $per_page ) ); $this->_column_headers = array( $columns, $hidden, $sortable ); $this->items = $all_items; } /** * Define hidden columns * * @return array */ public function get_hidden_columns() { return array(); } /** * Sort the data * * @param mixed $a * @param mixed $b * * @return int */ private function sort_data( $a, $b ) { // Set defaults $orderby = ( ! empty( $_GET['orderby'] ) ) ? sanitize_sql_orderby( wp_unslash( $_GET['orderby'] ) ) : 'post_title'; $order = ( ! empty( $_GET['order'] ) ) ? sanitize_sql_orderby( wp_unslash( $_GET['order'] ) ) : 'asc'; $result = strnatcasecmp( $a[ $orderby ], $b[ $orderby ] ); return ( $order === 'asc' ) ? $result : - $result; } }