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-actions.php
231 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Additional functions used in classes and another subclasses |
| 5 | */ |
| 6 | |
| 7 | class Permalink_Manager_Actions extends Permalink_Manager_Class { |
| 8 | |
| 9 | public function __construct() {} |
| 10 | |
| 11 | /** |
| 12 | * Find & replace (bulk action) |
| 13 | */ |
| 14 | static function find_replace($find_and_replace_fields) { |
| 15 | global $wpdb; |
| 16 | |
| 17 | // Reset variables |
| 18 | $updated_slugs_count = 0; |
| 19 | $updated_array = array(); |
| 20 | $alert_type = $alert_content = $errors = $main_content = ''; |
| 21 | $old_uris = get_option('permalink-manager-uris', array()); |
| 22 | |
| 23 | // Prepare default variables from $_POST object |
| 24 | $old_string = esc_sql($_POST['permalink-manager']['find-replace']['old_string']); |
| 25 | $new_string = esc_sql($_POST['permalink-manager']['find-replace']['new_string']); |
| 26 | $mode = isset($_POST['permalink-manager']['find-replace']['variant']) ? $_POST['permalink-manager']['find-replace']['variant'] : array('slugs'); |
| 27 | $post_types_array = ($_POST['permalink-manager']['find-replace']['post_types']); |
| 28 | $post_statuses_array = ($_POST['permalink-manager']['find-replace']['post_statuses']); |
| 29 | $post_types = implode("', '", $post_types_array); |
| 30 | $post_statuses = implode("', '", $post_statuses_array); |
| 31 | |
| 32 | // Save the rows before they are updated to an array |
| 33 | //$posts_to_update = $wpdb->get_results("SELECT post_title, post_name, ID FROM {$wpdb->posts} WHERE post_status IN ('{$post_statuses}') AND post_name LIKE '%{$old_string}%' AND post_type IN ('{$post_types}')", ARRAY_A); |
| 34 | $posts_to_update = $wpdb->get_results("SELECT post_title, post_name, ID FROM {$wpdb->posts} WHERE post_status IN ('{$post_statuses}') AND post_type IN ('{$post_types}')", ARRAY_A); |
| 35 | |
| 36 | // Now if the array is not empty use IDs from each subarray as a key |
| 37 | if($posts_to_update && empty($errors)) { |
| 38 | foreach ($posts_to_update as $row) { |
| 39 | |
| 40 | // Prepare variables |
| 41 | $old_post_name = $row['post_name']; |
| 42 | $old_default_uri = trim(str_replace(home_url("/"), "", get_permalink($row['ID'])), "/"); |
| 43 | $new_default_uri = Permalink_Manager_Helper_Functions::get_uri($row['ID'], true); |
| 44 | $old_uri = (isset($old_uris[$row['ID']])) ? $old_uris[$row['ID']] : $new_default_uri; |
| 45 | $old_slug = (strpos($old_uri, '/') !== false) ? substr($old_uri, strrpos($old_uri, '/') + 1) : $old_uri; |
| 46 | $old_base = (strpos($old_uri, '/') !== false) ? substr($old_uri, 0, strrpos( $old_uri, '/') ) : ''; |
| 47 | |
| 48 | // Process URI & slug |
| 49 | $new_slug = str_replace($old_string, $new_string, $old_slug); |
| 50 | $new_base = str_replace($old_string, $new_string, $old_base); |
| 51 | $new_uri = (in_array('both', $mode)) ? trim("{$new_base}/{$new_slug}", "/") : trim("{$old_base}/{$new_slug}", "/"); |
| 52 | $new_post_name = (in_array('post_names', $mode)) ? str_replace($old_string, $new_string, $old_post_name) : $old_post_name; // Post name is changed only in first mode |
| 53 | |
| 54 | // Check if native slug should be changed |
| 55 | if(in_array('post_names', $mode) && ($old_post_name != $new_post_name)) { |
| 56 | Permalink_Manager_Helper_Functions::update_slug_by_id($new_post_name, $row['ID']); |
| 57 | } |
| 58 | |
| 59 | if(($old_uri != $new_uri) || ($old_post_name != $new_post_name)) { |
| 60 | $old_uris[$row['ID']] = $new_uri; |
| 61 | $updated_array[] = array('post_title' => $row['post_title'], 'ID' => $row['ID'], 'old_uri' => $old_uri, 'new_uri' => $new_uri, 'old_slug' => $old_post_name, 'new_slug' => $new_post_name); |
| 62 | $updated_slugs_count++; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Filter array before saving |
| 67 | $old_uris = array_filter($old_uris); |
| 68 | update_option('permalink-manager-uris', $old_uris); |
| 69 | |
| 70 | $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count); |
| 71 | wp_reset_postdata(); |
| 72 | } |
| 73 | |
| 74 | return ($output) ? $output : ""; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Regenerate slugs & bases (bulk action) |
| 79 | */ |
| 80 | static function regenerate_all_permalinks() { |
| 81 | // Setup needed variables |
| 82 | $updated_slugs_count = 0; |
| 83 | $updated_array = array(); |
| 84 | $alert_type = $alert_content = $errors = $main_content = ''; |
| 85 | |
| 86 | $post_types_array = ($_POST['permalink-manager']['regenerate_slugs']['post_types']); |
| 87 | $post_statuses_array = ($_POST['permalink-manager']['regenerate_slugs']['post_statuses']); |
| 88 | $mode = isset($_POST['permalink-manager']['regenerate_slugs']['variant']) ? $_POST['permalink-manager']['regenerate_slugs']['variant'] : array('slugs'); |
| 89 | |
| 90 | $old_uris = get_option('permalink-manager-uris', array()); |
| 91 | $all_permastructs = get_option('permalink-manager-permastructs', array()); |
| 92 | |
| 93 | // Reset query |
| 94 | $reset_query = new WP_Query( array( 'post_type' => $post_types_array, 'post_status' => $post_statuses_array, 'posts_per_page' => -1 ) ); |
| 95 | |
| 96 | // The Loop |
| 97 | if ( $reset_query->have_posts() ) { |
| 98 | while ( $reset_query->have_posts() ) { |
| 99 | $reset_query->the_post(); |
| 100 | $post_id = get_the_ID(); |
| 101 | $this_post = get_post($post_id); |
| 102 | $updated = 0; |
| 103 | |
| 104 | // Prepare permastructs |
| 105 | $default_permastruct = Permalink_Manager_Helper_Functions::get_default_permastruct($this_post->post_type); |
| 106 | $custom_permastruct = isset($all_permastructs[$this_post->post_type]) ? $all_permastructs[$this_post->post_type] : $default_permastruct; |
| 107 | |
| 108 | // Prepare variables |
| 109 | $old_post_name = $this_post->post_name; |
| 110 | $old_default_uri = trim(str_replace(home_url("/"), "", get_permalink($post_id)), "/"); |
| 111 | $new_default_uri = Permalink_Manager_Helper_Functions::get_uri($post_id, true); |
| 112 | $old_uri = isset($old_uris[$post_id]) ? trim($old_uris[$post_id], "/") : $old_default_uri; |
| 113 | $old_slug = (strpos($old_uri, '/') !== false) ? substr($old_uri, strrpos($old_uri, '/') + 1) : $old_uri; |
| 114 | $correct_slug = sanitize_title(get_the_title($post_id)); |
| 115 | |
| 116 | // Process URI & slug |
| 117 | $new_slug = wp_unique_post_slug($correct_slug, $post_id, get_post_status($post_id), get_post_type($post_id), null); |
| 118 | $new_post_name = (in_array('post_names', $mode)) ? $new_slug : $old_post_name; // Post name is changed only in first mode |
| 119 | $new_uri = (in_array('both', $mode)) ? $new_default_uri : str_replace($old_slug, $new_slug, $old_uri); |
| 120 | |
| 121 | // Check if native slug should be changed |
| 122 | if(in_array('post_names', $mode) && ($old_post_name != $new_post_name)) { |
| 123 | Permalink_Manager_Helper_Functions::update_slug_by_id($new_post_name, $post_id); |
| 124 | } |
| 125 | |
| 126 | if(($old_uri != $new_uri) || ($old_post_name != $new_post_name)) { |
| 127 | $old_uris[$post_id] = $new_uri; |
| 128 | $updated_array[] = array('post_title' => get_the_title(), 'ID' => $post_id, 'old_uri' => $old_uri, 'new_uri' => $new_uri, 'old_slug' => $old_post_name, 'new_slug' => $new_post_name); |
| 129 | $updated_slugs_count++; |
| 130 | } |
| 131 | |
| 132 | // Do not store default values |
| 133 | if($new_uri == $old_default_uri && is_array($old_uris)) { |
| 134 | unset($old_uris[$post_id]); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Filter array before saving |
| 139 | $old_uris = array_filter($old_uris); |
| 140 | update_option('permalink-manager-uris', $old_uris); |
| 141 | |
| 142 | $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count); |
| 143 | wp_reset_postdata(); |
| 144 | } |
| 145 | |
| 146 | return ($output) ? $output : ""; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Update all slugs & bases (bulk action) |
| 151 | */ |
| 152 | static function update_all_permalinks() { |
| 153 | // Setup needed variables |
| 154 | $updated_slugs_count = 0; |
| 155 | $updated_array = array(); |
| 156 | $alert_type = $alert_content = $errors = $main_content = ''; |
| 157 | |
| 158 | $old_uris = get_option('permalink-manager-uris', array()); |
| 159 | $new_uris = isset($_POST['uri']) ? $_POST['uri'] : array(); |
| 160 | |
| 161 | // Double check if the slugs and ids are stored in arrays |
| 162 | if (!is_array($new_uris)) $new_uris = explode(',', $new_uris); |
| 163 | |
| 164 | if (!empty($new_uris)) { |
| 165 | foreach($new_uris as $id => $new_uri) { |
| 166 | // Prepare variables |
| 167 | $this_post = get_post($id); |
| 168 | $updated = ''; |
| 169 | |
| 170 | // Prepare old values |
| 171 | $old_default_uri = trim(str_replace(home_url("/"), "", get_permalink($id)), "/"); |
| 172 | $old_uri = isset($old_uris[$id]) ? trim($old_uris[$id], "/") : $old_default_uri; |
| 173 | |
| 174 | // Process new values; Empty entries will be treated as default values |
| 175 | $new_default_uri = Permalink_Manager_Helper_Functions::get_uri($id, true); |
| 176 | $new_uri = preg_replace('/\s+/', '', $new_uri); |
| 177 | $new_uri = ($new_uri) ? trim($new_uri, "/") : $new_default_uri; |
| 178 | $new_slug = (strpos($new_uri, '/') !== false) ? substr($new_uri, strrpos($new_uri, '/') + 1) : $new_uri; |
| 179 | |
| 180 | // Neither base nor slug was changed - continue |
| 181 | if($new_uri == $old_uri) continue; |
| 182 | |
| 183 | if($new_uri != $old_uri) { |
| 184 | $old_uris[$id] = $new_uri; |
| 185 | $updated_array[] = array('post_title' => get_the_title($id), 'ID' => $id, 'old_uri' => $old_uri, 'new_uri' => $new_uri); |
| 186 | $updated_slugs_count++; |
| 187 | } |
| 188 | |
| 189 | // Do not store default values |
| 190 | if($new_uri == $old_default_uri) { |
| 191 | unset($old_uris[$id]); |
| 192 | } |
| 193 | |
| 194 | } |
| 195 | |
| 196 | // Filter array before saving |
| 197 | $old_uris = array_filter($old_uris); |
| 198 | update_option('permalink-manager-uris', $old_uris); |
| 199 | |
| 200 | $output = array('updated' => $updated_array, 'updated_count' => $updated_slugs_count); |
| 201 | } |
| 202 | |
| 203 | return ($output) ? $output : ""; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Update permastructs |
| 208 | */ |
| 209 | static function update_permastructs() { |
| 210 | // Setup needed variables |
| 211 | $alert_type = $alert_content = $errors = $main_content = ''; |
| 212 | $permastructs = get_option('permalink-manager-permastructs', array()); |
| 213 | $new_permastructs = array_filter($_POST['permalink-manager']['custom-permastructs']); |
| 214 | |
| 215 | foreach($new_permastructs as $post_type => $new_permstruct) { |
| 216 | $default_permastruct = Permalink_Manager_Helper_Functions::get_default_permastruct($post_type, true); |
| 217 | $permastructs[$post_type] = trim(preg_replace('/\s+/', '', $new_permstruct), "/"); |
| 218 | |
| 219 | // Do not save default permastructs |
| 220 | if($default_permastruct == $new_permstruct) { |
| 221 | unset($permastructs[$post_type]); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | update_option('permalink-manager-permastructs', $permastructs); |
| 226 | |
| 227 | return ""; |
| 228 | } |
| 229 | |
| 230 | } |
| 231 |