permalink-manager-actions.php
8 years ago
permalink-manager-admin-functions.php
8 years ago
permalink-manager-core-functions.php
8 years ago
permalink-manager-helper-functions.php
8 years ago
permalink-manager-third-parties.php
8 years ago
permalink-manager-uri-functions-post.php
8 years ago
permalink-manager-third-parties.php
367 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Third parties integration |
| 5 | */ |
| 6 | class Permalink_Manager_Third_Parties extends Permalink_Manager_Class { |
| 7 | |
| 8 | public function __construct() { |
| 9 | add_action('init', array($this, 'init_hooks'), 99); |
| 10 | } |
| 11 | |
| 12 | function init_hooks() { |
| 13 | global $sitepress_settings, $permalink_manager_options, $polylang; |
| 14 | |
| 15 | // 1. WPML & Polylang |
| 16 | if($sitepress_settings || !empty($polylang->links_model->options)) { |
| 17 | // Detect Post/Term function |
| 18 | add_filter('permalink-manager-detected-post-id', array($this, 'wpml_language_mismatch_fix'), 9, 3); |
| 19 | add_filter('permalink-manager-detected-term-id', array($this, 'wpml_language_mismatch_fix'), 9, 3); |
| 20 | |
| 21 | // URI Editor |
| 22 | add_filter('permalink-manager-uri-editor-extra-info', array($this, 'wpml_lang_column_content_uri_editor'), 9, 3); |
| 23 | |
| 24 | if((isset($sitepress_settings['language_negotiation_type']) && $sitepress_settings['language_negotiation_type'] == 1) || (isset($polylang->links_model->options['force_lang']) && $polylang->links_model->options['force_lang'] == 1)) { |
| 25 | add_filter('permalink-manager-detect-uri', array($this, 'wpml_detect_post'), 9, 3); |
| 26 | add_filter('permalink-manager-post-permalink-prefix', array($this, 'wpml_element_lang_prefix'), 9, 3); |
| 27 | add_filter('permalink-manager-term-permalink-prefix', array($this, 'wpml_element_lang_prefix'), 9, 3); |
| 28 | add_filter('template_redirect', array($this, 'wpml_redirect'), 0, 998 ); |
| 29 | } else if(isset($sitepress_settings['language_negotiation_type']) && $sitepress_settings['language_negotiation_type'] == 3) { |
| 30 | add_filter('permalink-manager-detect-uri', array($this, 'wpml_ignore_lang_query_parameter'), 9); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // 2. AMP |
| 35 | if(defined('AMP_QUERY_VAR')) { |
| 36 | // Detect AMP endpoint |
| 37 | add_filter('permalink-manager-detect-uri', array($this, 'detect_amp'), 10, 2); |
| 38 | add_filter('request', array($this, 'enable_amp'), 10, 1); |
| 39 | } |
| 40 | |
| 41 | // 4. WP All Import |
| 42 | add_action('pmxi_after_xml_import', array($this, 'pmxi_fix_permalinks'), 10); |
| 43 | |
| 44 | // 5. WooCommerce |
| 45 | if(class_exists('WooCommerce')) { |
| 46 | add_filter('request', array($this, 'woocommerce_detect'), 9, 1); |
| 47 | add_filter('template_redirect', array($this, 'woocommerce_checkout_fix'), 9); |
| 48 | } |
| 49 | |
| 50 | // 6. Theme My Login |
| 51 | if(class_exists('Theme_My_Login')) { |
| 52 | add_filter('permalink_manager_filter_final_post_permalink', array($this, 'tml_keep_query_parameters'), 9, 3); |
| 53 | } |
| 54 | |
| 55 | // 7. WC_Voucher |
| 56 | if(class_exists('WC_Voucher')) { |
| 57 | add_filter('permalink-manager-disabled-post-types', array($this, 'disabled_post_types'), 0, 3); |
| 58 | } |
| 59 | |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * 1. WPML filters |
| 64 | */ |
| 65 | function wpml_language_mismatch_fix($item_id, $uri_parts, $is_term = false) { |
| 66 | global $wp, $language_code; |
| 67 | |
| 68 | if($is_term) { |
| 69 | $current_term = get_term($item_id); |
| 70 | $element_type = (!empty($current_term) && !is_wp_error($current_term)) ? $current_term->taxonomy : ""; |
| 71 | } else { |
| 72 | $element_type = get_post_type($item_id); |
| 73 | } |
| 74 | $language_code = apply_filters('wpml_element_language_code', null, array('element_id' => $item_id, 'element_type' => $element_type)); |
| 75 | |
| 76 | if(!empty($uri_parts['lang']) && ($uri_parts['lang'] != $language_code)) { |
| 77 | $wpml_item_id = apply_filters('wpml_object_id', $item_id); |
| 78 | $item_id = (is_numeric($wpml_item_id)) ? $wpml_item_id : $item_id; |
| 79 | } |
| 80 | |
| 81 | return $item_id; |
| 82 | } |
| 83 | |
| 84 | function wpml_detect_post($uri_parts, $request_url, $endpoints) { |
| 85 | global $sitepress_settings, $polylang; |
| 86 | |
| 87 | if(!empty($sitepress_settings['active_languages'])) { |
| 88 | $languages_list = implode("|", $sitepress_settings['active_languages']); |
| 89 | } elseif(function_exists('pll_languages_list')) { |
| 90 | $languages_array = pll_languages_list(); |
| 91 | $languages_list = (is_array($languages_array)) ? implode("|", $languages_array) : ""; |
| 92 | } |
| 93 | |
| 94 | if(!empty($languages_list)) { |
| 95 | preg_match("/^(?:({$languages_list})\/)?(.+?)(?|\/({$endpoints})\/?([^\/]*)|\/()([\d+]))?\/?$/i", $request_url, $regex_parts); |
| 96 | |
| 97 | $uri_parts['lang'] = (!empty($regex_parts[1])) ? $regex_parts[1] : ""; |
| 98 | $uri_parts['uri'] = (!empty($regex_parts[2])) ? $regex_parts[2] : ""; |
| 99 | $uri_parts['endpoint'] = (!empty($regex_parts[3])) ? $regex_parts[3] : ""; |
| 100 | $uri_parts['endpoint_value'] = (!empty($regex_parts[4])) ? $regex_parts[4] : ""; |
| 101 | } |
| 102 | |
| 103 | return $uri_parts; |
| 104 | } |
| 105 | |
| 106 | function wpml_element_lang_prefix($prefix, $element, $edit_uri_box = false) { |
| 107 | global $sitepress_settings, $polylang; |
| 108 | |
| 109 | if(isset($element->post_type)) { |
| 110 | $post = (is_integer($element)) ? get_post($element) : $element; |
| 111 | $element_id = $post->ID; |
| 112 | $element_type = $post->post_type; |
| 113 | } else { |
| 114 | $term = (is_numeric($element)) ? get_term(intval($element)) : $element; |
| 115 | $element_id = $term->term_id; |
| 116 | $element_type = $term->taxonomy; |
| 117 | } |
| 118 | |
| 119 | $language_code = apply_filters( 'wpml_element_language_code', null, array('element_id' => $element_id, 'element_type' => $element_type)); |
| 120 | |
| 121 | if($edit_uri_box) { |
| 122 | // Last instance - use language paramater from &_GET array |
| 123 | $language_code = (empty($language_code) && !empty($_GET['lang'])) ? $_GET['lang'] : $language_code; |
| 124 | } |
| 125 | |
| 126 | // Append slash to the end of language code if it is not empty |
| 127 | if(!empty($language_code)) { |
| 128 | $prefix = "{$language_code}/"; |
| 129 | |
| 130 | // Hide language code if "Use directory for default language" option is enabled |
| 131 | $default_language = Permalink_Manager_Helper_Functions::get_language(); |
| 132 | $hide_prefix_for_default_lang = ((isset($sitepress_settings['urls']['directory_for_default_language']) && $sitepress_settings['urls']['directory_for_default_language'] != 1) || !empty($polylang->links_model->options['hide_default'])) ? true : false; |
| 133 | |
| 134 | if($hide_prefix_for_default_lang && ($default_language == $language_code)) { |
| 135 | $prefix = ""; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return $prefix; |
| 140 | } |
| 141 | |
| 142 | function wpml_lang_column_uri_editor($columns) { |
| 143 | if(class_exists('SitePress') || class_exists('Polylang')) { |
| 144 | $columns['post_lang'] = __('Language', 'permalink-manager'); |
| 145 | } |
| 146 | |
| 147 | return $columns; |
| 148 | } |
| 149 | |
| 150 | function wpml_lang_column_content_uri_editor($output, $column, $element) { |
| 151 | if(isset($element->post_type)) { |
| 152 | $post = (is_integer($element)) ? get_post($element) : $element; |
| 153 | $element_id = $post->ID; |
| 154 | $element_type = $post->post_type; |
| 155 | } else { |
| 156 | $term = (is_numeric($element)) ? get_term(intval($element)) : $element; |
| 157 | $element_id = $term->term_id; |
| 158 | $element_type = $term->taxonomy; |
| 159 | } |
| 160 | |
| 161 | $language_code = apply_filters( 'wpml_element_language_code', null, array('element_id' => $element_id, 'element_type' => $element_type)); |
| 162 | $output .= (!empty($language_code)) ? sprintf(" | <span><strong>%s:</strong> %s</span>", __("Language"), $language_code) : ""; |
| 163 | |
| 164 | return $output; |
| 165 | } |
| 166 | |
| 167 | function wpml_ignore_lang_query_parameter($uri_parts) { |
| 168 | global $permalink_manager_uris; |
| 169 | |
| 170 | foreach($permalink_manager_uris as &$uri) { |
| 171 | $uri = trim(strtok($uri, '?'), "/"); |
| 172 | } |
| 173 | |
| 174 | return $uri_parts; |
| 175 | } |
| 176 | |
| 177 | function wpml_redirect() { |
| 178 | global $language_code, $wp_query; |
| 179 | |
| 180 | if(!empty($language_code) && defined('ICL_LANGUAGE_CODE') && ICL_LANGUAGE_CODE != $language_code && !empty($wp_query->query['do_not_redirect'])) { |
| 181 | unset($wp_query->query['do_not_redirect']); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * 2. AMP hooks |
| 187 | */ |
| 188 | function detect_amp($uri_parts, $request_url) { |
| 189 | global $amp_enabled; |
| 190 | $amp_query_var = AMP_QUERY_VAR; |
| 191 | |
| 192 | // Check if AMP should be triggered |
| 193 | preg_match("/^(.+?)\/?({$amp_query_var})?\/?$/i", $uri_parts['uri'], $regex_parts); |
| 194 | if(!empty($regex_parts[2])) { |
| 195 | $uri_parts['uri'] = $regex_parts[1]; |
| 196 | $amp_enabled = true; |
| 197 | } |
| 198 | |
| 199 | return $uri_parts; |
| 200 | } |
| 201 | |
| 202 | function enable_amp($query) { |
| 203 | global $amp_enabled; |
| 204 | |
| 205 | if(!empty($amp_enabled)) { |
| 206 | $query[AMP_QUERY_VAR] = 1; |
| 207 | } |
| 208 | |
| 209 | return $query; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * 3. Custom Permalinks |
| 214 | */ |
| 215 | public static function custom_permalinks_uris() { |
| 216 | global $wpdb; |
| 217 | |
| 218 | $custom_permalinks_uris = array(); |
| 219 | |
| 220 | // 1. List tags/categories |
| 221 | $table = get_option('custom_permalink_table'); |
| 222 | if($table && is_array($table)) { |
| 223 | foreach ( $table as $permalink => $info ) { |
| 224 | $custom_permalinks_uris[] = array( |
| 225 | 'id' => "tax-" . $info['id'], |
| 226 | 'uri' => trim($permalink, "/") |
| 227 | ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // 2. List posts/pages |
| 232 | $query = "SELECT p.ID, m.meta_value FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS m ON (p.ID = m.post_id) WHERE m.meta_key = 'custom_permalink' AND m.meta_value != '';"; |
| 233 | $posts = $wpdb->get_results($query); |
| 234 | foreach($posts as $post) { |
| 235 | $custom_permalinks_uris[] = array( |
| 236 | 'id' => $post->ID, |
| 237 | 'uri' => trim($post->meta_value, "/"), |
| 238 | ); |
| 239 | } |
| 240 | |
| 241 | return $custom_permalinks_uris; |
| 242 | } |
| 243 | |
| 244 | static public function import_custom_permalinks_uris() { |
| 245 | global $permalink_manager_uris, $permalink_manager_before_sections_html; |
| 246 | |
| 247 | $custom_permalinks_plugin = 'custom-permalinks/custom-permalinks.php'; |
| 248 | |
| 249 | if(is_plugin_active($custom_permalinks_plugin) && !empty($_POST['disable_custom_permalinks'])) { |
| 250 | deactivate_plugins($custom_permalinks_plugin); |
| 251 | } |
| 252 | |
| 253 | // Get a list of imported URIs |
| 254 | $custom_permalinks_uris = self::custom_permalinks_uris(); |
| 255 | |
| 256 | if(!empty($custom_permalinks_uris) && count($custom_permalinks_uris) > 0) { |
| 257 | foreach($custom_permalinks_uris as $item) { |
| 258 | $permalink_manager_uris[$item['id']] = $item['uri']; |
| 259 | } |
| 260 | |
| 261 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( '"Custom Permalinks" URIs were imported!', 'permalink-manager' ), 'updated'); |
| 262 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 263 | } else { |
| 264 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'No "Custom Permalinks" URIs were imported!', 'permalink-manager' ), 'error'); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * 4. WP All Import |
| 270 | */ |
| 271 | function pmxi_fix_permalinks($import_id) { |
| 272 | global $permalink_manager_uris, $wpdb; |
| 273 | |
| 274 | $post_ids = $wpdb->get_col($wpdb->prepare("SELECT post_id FROM {$wpdb->prefix}pmxi_posts WHERE import_id = %s", $import_id)); |
| 275 | |
| 276 | // Just in case |
| 277 | sleep(3); |
| 278 | |
| 279 | if(array($post_ids)) { |
| 280 | foreach($post_ids as $id) { |
| 281 | // Continue only if no custom URI is already assigned |
| 282 | if(!empty($permalink_manager_uris[$id])) { continue; } |
| 283 | |
| 284 | // Get default post URI |
| 285 | $new_uri = Permalink_Manager_URI_Functions_Post::get_default_post_uri($id); |
| 286 | $permalink_manager_uris[$id] = $new_uri; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * 5. WooCommerce |
| 295 | */ |
| 296 | function woocommerce_detect($query) { |
| 297 | global $woocommerce, $pm_item_id; |
| 298 | |
| 299 | // Fix shop page |
| 300 | if(is_numeric($pm_item_id) && get_option('woocommerce_shop_page_id') == $pm_item_id) { |
| 301 | $query['post_type'] = 'product'; |
| 302 | unset($query['pagename']); |
| 303 | } |
| 304 | |
| 305 | // Fix WooCommerce pages |
| 306 | if(!empty($woocommerce->query->query_vars)) { |
| 307 | $query_vars = $woocommerce->query->query_vars; |
| 308 | |
| 309 | foreach($query_vars as $key => $val) { |
| 310 | if(isset($query[$key])) { |
| 311 | $woocommerce_page = true; |
| 312 | $query['do_not_redirect'] = 1; |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return $query; |
| 319 | } |
| 320 | |
| 321 | function woocommerce_checkout_fix() { |
| 322 | global $wp_query, $pm_item_id, $permalink_manager_options; |
| 323 | |
| 324 | // Redirect from Shop archive to selected page |
| 325 | if(is_shop() && empty($pm_item_id)) { |
| 326 | $redirect_mode = (!empty($permalink_manager_options['general']['redirect'])) ? $permalink_manager_options['general']['redirect'] : false; |
| 327 | $redirect_shop = apply_filters('permalink-manager-redirect-shop-archive', false); |
| 328 | $shop_page = get_option('woocommerce_shop_page_id'); |
| 329 | |
| 330 | if($redirect_mode && $redirect_shop && $shop_page && empty($wp_query->query_vars['s'])) { |
| 331 | $shop_url = get_permalink($shop_page); |
| 332 | wp_safe_redirect($shop_url, $redirect_mode); |
| 333 | exit(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // Do not redirect "thank you" & another WooCommerce pages |
| 338 | if(is_checkout() || is_wc_endpoint_url()) { |
| 339 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * 6. Theme My Login |
| 345 | */ |
| 346 | function tml_keep_query_parameters($permalink, $post, $old_permalink) { |
| 347 | // Get the query string from old permalink |
| 348 | $get_parameters = (($pos = strpos($old_permalink, "?")) !== false) ? substr($old_permalink, $pos) : ""; |
| 349 | |
| 350 | return $permalink . $get_parameters; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * 7. Disabled post types (WC_Voucher) |
| 355 | */ |
| 356 | function disabled_post_types($post_types) { |
| 357 | if(!is_array($post_types)) { return $post_types; } |
| 358 | |
| 359 | $post_types[] = "wc_voucher"; |
| 360 | |
| 361 | return $post_types; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | } |
| 366 | ?> |
| 367 |