permalink-manager-actions.php
9 years ago
permalink-manager-admin-functions.php
9 years ago
permalink-manager-helper-functions.php
9 years ago
permalink-manager-uri-functions-post.php
9 years ago
permalink-manager-actions.php
196 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Additional hooks for "Permalink Manager Pro" |
| 4 | */ |
| 5 | class Permalink_Manager_Actions extends Permalink_Manager_Class { |
| 6 | |
| 7 | public function __construct() { |
| 8 | add_action( 'admin_init', array($this, 'trigger_action'), 999 ); |
| 9 | add_action( 'admin_init', array($this, 'clear_uris') ); |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Actions |
| 14 | */ |
| 15 | public function trigger_action() { |
| 16 | global $permalink_manager_before_sections_html, $permalink_manager_after_sections_html; |
| 17 | |
| 18 | // 1. Check if the form was submitted (make exception for clear sitemap cache function) |
| 19 | if(isset($_REQUEST['flush_sitemaps'])) { |
| 20 | $this->flush_sitemaps(); |
| 21 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'Sitemap was updated!', 'permalink-manager' ), 'updated'); |
| 22 | |
| 23 | return; |
| 24 | } else if(empty($_POST)) { return; } |
| 25 | |
| 26 | $actions_map = array( |
| 27 | 'uri_editor' => array('function' => 'update_all_permalinks', 'display_uri_table' => true), |
| 28 | 'regenerate' => array('function' => 'regenerate_all_permalinks', 'display_uri_table' => true), |
| 29 | 'find_and_replace' => array('function' => 'find_and_replace', 'display_uri_table' => true), |
| 30 | 'permalink_manager_options' => array('function' => 'save_settings'), |
| 31 | 'permalink_manager_permastructs' => array('function' => 'save_permastructures'), |
| 32 | 'flush_sitemaps' => array('function' => 'save_permastructures'), |
| 33 | ); |
| 34 | // Clear URIs & reset settings & permastructs also should be added here. |
| 35 | |
| 36 | // 2. Find the action |
| 37 | foreach($actions_map as $action => $map) { |
| 38 | if(isset($_POST[$action]) && wp_verify_nonce($_POST[$action], 'permalink-manager')) { |
| 39 | $output = call_user_func(array($this, $map['function'])); |
| 40 | |
| 41 | // Get list of updated URIs |
| 42 | if(!empty($map['display_uri_table'])) { |
| 43 | $updated_slugs_count = (isset($output['updated_count']) && $output['updated_count'] > 0) ? $output['updated_count'] : false; |
| 44 | $updated_slugs_array = ($updated_slugs_count) ? $output['updated'] : ''; |
| 45 | } |
| 46 | |
| 47 | // Trigger only one function |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // 3. Display the slugs table (and append the globals) |
| 53 | if(isset($updated_slugs_count)) { |
| 54 | if($updated_slugs_count > 0) { |
| 55 | $updated_title = __('List of updated items', 'bis'); |
| 56 | $alert_content = sprintf( _n( '<strong>%d</strong> slug was updated!', '<strong>%d</strong> slugs were updated!', $updated_slugs_count, 'permalink-manager' ), $updated_slugs_count ) . ' '; |
| 57 | $alert_content .= sprintf( __( '<a %s>Click here</a> to go to the list of updated slugs', 'permalink-manager' ), "href=\"#TB_inline?width=100%&height=600&inlineId=updated-list\" title=\"{$updated_title}\" class=\"thickbox\""); |
| 58 | |
| 59 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message($alert_content, 'updated'); |
| 60 | $permalink_manager_after_sections_html .= Permalink_Manager_Admin_Functions::display_updated_slugs($updated_slugs_array); |
| 61 | } else { |
| 62 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( '<strong>No slugs</strong> were updated!', 'permalink-manager' ), 'error'); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Save settings |
| 69 | */ |
| 70 | public static function save_settings() { |
| 71 | global $permalink_manager_options; |
| 72 | |
| 73 | $post_fields = $_POST; |
| 74 | $new_options = array(); |
| 75 | |
| 76 | foreach($post_fields as $option_name => $option_value) { |
| 77 | $new_options[$option_name] = $option_value; |
| 78 | } |
| 79 | |
| 80 | // Override the global with settings |
| 81 | $permalink_manager_options = $new_options = array_filter($new_options); |
| 82 | |
| 83 | // Save the settings in database |
| 84 | update_option('permalink-manager', $new_options); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Save permastructures |
| 89 | */ |
| 90 | public static function save_permastructures() { |
| 91 | global $permalink_manager_permastructs; |
| 92 | |
| 93 | $post_fields = $_POST; |
| 94 | $new_options = array(); |
| 95 | |
| 96 | foreach($post_fields as $option_name => $option_value) { |
| 97 | $new_options[$option_name] = $option_value; |
| 98 | } |
| 99 | |
| 100 | // Trim the trailing slashes & remove empty permastructures |
| 101 | $new_options = Permalink_Manager_Helper_Functions::multidimensional_array_map('untrailingslashit', $new_options); |
| 102 | foreach($new_options as $group_name => $group) { |
| 103 | if(is_array($group)) { |
| 104 | foreach($group as $element => $permastruct) { |
| 105 | // Trim slashes |
| 106 | $permastruct = trim($permastruct, "/"); |
| 107 | |
| 108 | // Do not store default permastructures |
| 109 | $default_permastruct = ($group_name == 'post_types') ? Permalink_Manager_Helper_Functions::get_default_permastruct($element, true) : ""; |
| 110 | if($permastruct == $default_permastruct) { unset($group[$element]); } |
| 111 | } |
| 112 | // Do not store empty permastructures |
| 113 | $new_options[$group_name] = array_filter($group); |
| 114 | } else { |
| 115 | unset($new_options[$group_name]); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Override the global with settings |
| 120 | $permalink_manager_permastructs = $new_options = array_filter($new_options); |
| 121 | |
| 122 | // Save the settings in database |
| 123 | update_option('permalink-manager-permastructs', $new_options); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Remove URI from options array after post is moved to the trash |
| 128 | */ |
| 129 | function clear_uris($post_id) { |
| 130 | global $permalink_manager_uris; |
| 131 | |
| 132 | if(isset($_GET['clear-permalink-manager-uris']) && !empty($permalink_manager_uris)) { |
| 133 | foreach($permalink_manager_uris as $post_id => $uri) { |
| 134 | // Loop only through post URIs |
| 135 | if(is_numeric($post_id)) { |
| 136 | $post_status = get_post_status($post_id); |
| 137 | if(in_array($post_status, array('auto-draft', 'trash', ''))) { |
| 138 | unset($permalink_manager_uris[$post_id]); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * "Find and replace" in "Tools" |
| 149 | */ |
| 150 | function find_and_replace() { |
| 151 | // Check if posts or terms should be updated |
| 152 | if(!empty($_POST['section_type']) && $_POST['section_type'] == 'tax') { |
| 153 | return Permalink_Manager_URI_Functions_Tax::find_and_replace(); |
| 154 | } else { |
| 155 | return Permalink_Manager_URI_Functions_Post::find_and_replace(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Regenerate all permalinks in "Tools" |
| 161 | */ |
| 162 | function regenerate_all_permalinks() { |
| 163 | // Check if posts or terms should be updated |
| 164 | if(!empty($_POST['section_type']) && $_POST['section_type'] == 'tax') { |
| 165 | return Permalink_Manager_URI_Functions_Tax::regenerate_all_permalinks(); |
| 166 | } else { |
| 167 | return Permalink_Manager_URI_Functions_Post::regenerate_all_permalinks(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Update all permalinks in "Permalink Editor" |
| 173 | */ |
| 174 | function update_all_permalinks() { |
| 175 | // Check if posts or terms should be updated |
| 176 | if(!empty($_POST['section_type']) && $_POST['section_type'] == 'tax') { |
| 177 | return Permalink_Manager_URI_Functions_Tax::update_all_permalinks(); |
| 178 | } else { |
| 179 | return Permalink_Manager_URI_Functions_Post::update_all_permalinks(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Clear sitemaps cache |
| 185 | */ |
| 186 | function flush_sitemaps($types = array()) { |
| 187 | // Reset sitemap's cache |
| 188 | if(class_exists('WPSEO_Sitemaps_Cache')) { |
| 189 | $sitemaps = WPSEO_Sitemaps_Cache::clear($types); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | |
| 195 | ?> |
| 196 |