permalink-manager-debug.php
3 months ago
permalink-manager-permastructures.php
1 month ago
permalink-manager-settings.php
3 months ago
permalink-manager-tools.php
3 months ago
permalink-manager-ui-elements.php
3 months ago
permalink-manager-uri-editor-post.php
1 month ago
permalink-manager-uri-editor.php
3 months ago
permalink-manager-uri-editor.php
157 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 | |
| 5 | /** |
| 6 | * Display Bulk URI Editor |
| 7 | */ |
| 8 | class Permalink_Manager_URI_Editor { |
| 9 | public $this_section = 'uri_editor'; |
| 10 | |
| 11 | public function __construct() { |
| 12 | add_filter( 'permalink_manager_sections', array( $this, 'add_admin_section' ), 0 ); |
| 13 | add_filter( 'screen_settings', array( $this, 'screen_options' ), 99, 2 ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Add a new section to the Permalink Manager UI |
| 18 | * |
| 19 | * @param array $admin_sections |
| 20 | * |
| 21 | * @return array |
| 22 | */ |
| 23 | public function add_admin_section( $admin_sections ) { |
| 24 | $admin_sections[ $this->this_section ] = array( |
| 25 | 'name' => __( 'URI Editor', 'permalink-manager' ) |
| 26 | ); |
| 27 | |
| 28 | // Display separate section for each post type |
| 29 | $post_types = Permalink_Manager_Helper_Functions::get_post_types_array( 'full' ); |
| 30 | foreach ( $post_types as $post_type_name => $post_type ) { |
| 31 | // Check if post type exists |
| 32 | if ( ! post_type_exists( $post_type_name ) ) { |
| 33 | continue; |
| 34 | } |
| 35 | |
| 36 | $icon = ( class_exists( 'WooCommerce' ) && $post_type_name == 'product' ) ? "<i class=\"woocommerce-icon woocommerce-cart\"></i>" : ""; |
| 37 | |
| 38 | $admin_sections[ $this->this_section ]['subsections'][ $post_type_name ] = array( |
| 39 | 'name' => "{$icon} {$post_type['label']}", |
| 40 | 'function' => array( 'class' => 'Permalink_Manager_URI_Editor_Post', 'method' => 'display_admin_section' ) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | // Permalink Manager Pro: Display separate section for each taxonomy |
| 45 | $taxonomies = Permalink_Manager_Helper_Functions::get_taxonomies_array( 'full' ); |
| 46 | foreach ( $taxonomies as $taxonomy_name => $taxonomy ) { |
| 47 | // Check if taxonomy exists |
| 48 | if ( ! taxonomy_exists( $taxonomy_name ) ) { |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | // Get the icon |
| 53 | $icon = ( class_exists( 'WooCommerce' ) && in_array( $taxonomy_name, array( 'product_tag', 'product_cat' ) ) ) ? "<i class=\"woocommerce-icon woocommerce-cart\"></i>" : "<i class=\"dashicons dashicons-tag\"></i>"; |
| 54 | |
| 55 | $admin_sections[ $this->this_section ]['subsections']["tax_{$taxonomy_name}"] = array( |
| 56 | 'name' => "{$icon} {$taxonomy['label']}", |
| 57 | 'html' => Permalink_Manager_UI_Elements::pro_text(), |
| 58 | 'pro' => true |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | // A little dirty hack to move wooCommerce product & taxonomies to the end of array |
| 63 | if ( class_exists( 'WooCommerce' ) ) { |
| 64 | foreach ( array( 'product', 'tax_product_tag', 'tax_product_cat' ) as $section_name ) { |
| 65 | if ( empty( $admin_sections[ $this->this_section ]['subsections'][ $section_name ] ) ) { |
| 66 | continue; |
| 67 | } |
| 68 | $section = $admin_sections[ $this->this_section ]['subsections'][ $section_name ]; |
| 69 | unset( $admin_sections[ $this->this_section ]['subsections'][ $section_name ] ); |
| 70 | $admin_sections[ $this->this_section ]['subsections'][ $section_name ] = $section; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return $admin_sections; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Display "Screen options" |
| 79 | * |
| 80 | * @param string $html |
| 81 | * @param string $screen |
| 82 | * |
| 83 | * @return string |
| 84 | */ |
| 85 | public function screen_options( $html, $screen ) { |
| 86 | global $active_section; |
| 87 | |
| 88 | // Display the screen options only in "Permalink Editor" |
| 89 | if ( $active_section != $this->this_section ) { |
| 90 | return $html; |
| 91 | } |
| 92 | |
| 93 | $button = get_submit_button( __( 'Apply', 'permalink-manager' ), 'primary', 'screen-options-apply', false ); |
| 94 | $html = "<fieldset class=\"permalink-manager-screen-options\">"; |
| 95 | |
| 96 | $screen_options = array( |
| 97 | 'per_page' => array( |
| 98 | 'type' => 'number', |
| 99 | 'label' => __( 'Per page', 'permalink-manager' ), |
| 100 | 'input_class' => 'settings-select' |
| 101 | ), |
| 102 | 'post_statuses' => array( |
| 103 | 'type' => 'checkbox', |
| 104 | 'label' => __( 'Post statuses', 'permalink-manager' ), |
| 105 | 'choices' => Permalink_Manager_Helper_Functions::get_post_statuses(), |
| 106 | 'select_all' => '', |
| 107 | 'unselect_all' => '', |
| 108 | ) |
| 109 | ); |
| 110 | |
| 111 | foreach ( $screen_options as $field_name => $field_args ) { |
| 112 | $field_args['container'] = 'screen-options'; |
| 113 | $html .= Permalink_Manager_UI_Elements::generate_option_field( "screen-options[{$field_name}]", $field_args ); |
| 114 | } |
| 115 | |
| 116 | $html .= sprintf( "</fieldset>%s", $button ); |
| 117 | |
| 118 | return $html; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Trigger SQL query to get all items |
| 123 | * |
| 124 | * @param $sql_parts |
| 125 | * @param $current_page |
| 126 | * @param $is_taxonomy |
| 127 | * |
| 128 | * @return array |
| 129 | */ |
| 130 | public static function prepare_sql_query( $sql_parts, $current_page, $is_taxonomy = false ) { |
| 131 | global $permalink_manager_options, $wpdb; |
| 132 | |
| 133 | $per_page = is_numeric( $permalink_manager_options['screen-options']['per_page'] ) ? $permalink_manager_options['screen-options']['per_page'] : 10; |
| 134 | $offset = ( $current_page - 1 ) * $per_page; |
| 135 | |
| 136 | // Prepare the count SQL query |
| 137 | $count_query_parts = $sql_parts; |
| 138 | $count_query_parts['start'] = preg_replace( '/(SELECT.*FROM)/', 'SELECT COUNT(*) FROM', $count_query_parts['start'] ); |
| 139 | $count_query = apply_filters( 'permalink_manager_filter_uri_editor_query', implode( "", $count_query_parts ), $count_query_parts, $is_taxonomy ); |
| 140 | $total_items_raw = $wpdb->get_var( $count_query ); |
| 141 | |
| 142 | // Pagination support |
| 143 | $sql_query = implode( "", $sql_parts ); |
| 144 | $sql_query .= sprintf( " LIMIT %d, %d", $offset, $per_page ); |
| 145 | |
| 146 | // Get items |
| 147 | $sql_query = apply_filters( 'permalink_manager_filter_uri_editor_query', $sql_query, $sql_parts, $is_taxonomy ); |
| 148 | $all_items_raw = $wpdb->get_results( $sql_query, ARRAY_A ); |
| 149 | |
| 150 | $total_items = ( is_numeric( $total_items_raw ) ) ? (int) $total_items_raw : 0; |
| 151 | $all_items = ( ! empty( $all_items_raw ) ) ? $all_items_raw : array(); |
| 152 | |
| 153 | return array( $all_items, $total_items, $per_page ); |
| 154 | } |
| 155 | |
| 156 | } |
| 157 |