permalink-manager-actions.php
5 years ago
permalink-manager-admin-functions.php
5 years ago
permalink-manager-core-functions.php
5 years ago
permalink-manager-debug.php
5 years ago
permalink-manager-gutenberg.php
5 years ago
permalink-manager-helper-functions.php
5 years ago
permalink-manager-language-plugins.php
5 years ago
permalink-manager-third-parties.php
5 years ago
permalink-manager-uri-functions-post.php
5 years ago
permalink-manager-uri-functions.php
5 years ago
permalink-manager-third-parties.php
1241 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 | add_action('plugins_loaded', array($this, 'init_early_hooks'), 99); |
| 11 | } |
| 12 | |
| 13 | function init_hooks() { |
| 14 | global $permalink_manager_options; |
| 15 | |
| 16 | // 0. Stop redirect |
| 17 | add_action('wp', array($this, 'stop_redirect'), 0); |
| 18 | |
| 19 | // 2. AMP |
| 20 | if(defined('AMP_QUERY_VAR')) { |
| 21 | // Detect AMP endpoint |
| 22 | add_filter('permalink_manager_detect_uri', array($this, 'detect_amp'), 10, 2); |
| 23 | add_filter('request', array($this, 'enable_amp'), 10, 1); |
| 24 | } |
| 25 | |
| 26 | // 4. WooCommerce |
| 27 | if(class_exists('WooCommerce')) { |
| 28 | add_filter('request', array($this, 'woocommerce_detect'), 20, 1); |
| 29 | add_filter('template_redirect', array($this, 'woocommerce_checkout_fix'), 9); |
| 30 | |
| 31 | if(class_exists('Permalink_Manager_Pro_Functions')) { |
| 32 | if(empty($permalink_manager_options['general']['partial_disable']['post_types']) || !in_array('shop_coupon', $permalink_manager_options['general']['partial_disable']['post_types'])) { |
| 33 | if(is_admin()) { |
| 34 | add_filter('woocommerce_coupon_data_tabs', 'Permalink_Manager_Pro_Functions::woocommerce_coupon_tabs'); |
| 35 | add_action('woocommerce_coupon_data_panels', 'Permalink_Manager_Pro_Functions::woocommerce_coupon_panel'); |
| 36 | add_action('woocommerce_coupon_options_save', 'Permalink_Manager_Pro_Functions::woocommerce_save_coupon_uri', 9, 2); |
| 37 | } |
| 38 | |
| 39 | add_filter('request', 'Permalink_Manager_Pro_Functions::woocommerce_detect_coupon_code', 1, 1); |
| 40 | } |
| 41 | |
| 42 | add_filter('permalink_manager_disabled_post_types', 'Permalink_Manager_Pro_Functions::woocommerce_coupon_uris', 9, 1); |
| 43 | } |
| 44 | |
| 45 | // WooCommerce Import/Export |
| 46 | add_filter('woocommerce_product_export_product_default_columns', array($this, 'woocommerce_csv_custom_uri_column'), 9); |
| 47 | add_filter('woocommerce_product_export_product_column_custom_uri', array($this, 'woocommerce_export_custom_uri_value'), 9, 3); |
| 48 | |
| 49 | add_filter('woocommerce_csv_product_import_mapping_options', array($this, 'woocommerce_csv_custom_uri_column'), 9); |
| 50 | add_filter('woocommerce_csv_product_import_mapping_default_columns', array($this, 'woocommerce_csv_custom_uri_column'), 9); |
| 51 | add_action('woocommerce_product_import_inserted_product_object', array($this, 'woocommerce_csv_import_custom_uri'), 9, 2); |
| 52 | |
| 53 | add_action('woocommerce_product_duplicate', array($this, 'woocommerce_generate_permalinks_after_duplicate'), 9, 2); |
| 54 | add_filter('permalink_manager_filter_default_post_uri', array($this, 'woocommerce_product_attributes'), 5, 5); |
| 55 | |
| 56 | if(wp_doing_ajax() && class_exists('SitePress')) { |
| 57 | add_filter('permalink_manager_filter_final_post_permalink', array($this, 'woocommerce_translate_ajax_fragments_urls'), 9999, 3); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // 5. Theme My Login |
| 62 | if(class_exists('Theme_My_Login')) { |
| 63 | add_filter('permalink_manager_filter_final_post_permalink', array($this, 'tml_keep_query_parameters'), 9, 3); |
| 64 | } |
| 65 | |
| 66 | // 6. Yoast SEO |
| 67 | add_filter('wpseo_xml_sitemap_post_url', array($this, 'yoast_fix_sitemap_urls'), 9); |
| 68 | if(defined('WPSEO_VERSION') && version_compare(WPSEO_VERSION, '14.0', '>=')) { |
| 69 | add_action('permalink_manager_updated_post_uri', array($this, 'yoast_update_indexable_permalink'), 10, 3); |
| 70 | add_action('permalink_manager_updated_term_uri', array($this, 'yoast_update_indexable_permalink'), 10, 3); |
| 71 | add_filter('wpseo_canonical', array($this, 'yoast_fix_canonical'), 10); |
| 72 | add_filter('wpseo_opengraph_url', array($this, 'yoast_fix_canonical'), 10); |
| 73 | } |
| 74 | |
| 75 | // 7. Breadcrumbs |
| 76 | add_filter('wpseo_breadcrumb_links', array($this, 'filter_breadcrumbs'), 9); |
| 77 | add_filter('rank_math/frontend/breadcrumb/items', array($this, 'filter_breadcrumbs'), 9); |
| 78 | add_filter('seopress_pro_breadcrumbs_crumbs', array($this, 'filter_breadcrumbs'), 9); |
| 79 | add_filter('woocommerce_get_breadcrumb', array($this, 'filter_breadcrumbs'), 9); |
| 80 | add_filter('slim_seo_breadcrumbs_links', array($this, 'filter_breadcrumbs'), 9); |
| 81 | |
| 82 | // 8. WooCommerce Wishlist Plugin |
| 83 | if(function_exists('tinv_get_option')) { |
| 84 | add_filter('permalink_manager_detect_uri', array($this, 'ti_woocommerce_wishlist_uris'), 15, 3); |
| 85 | } |
| 86 | |
| 87 | // 9. Revisionize |
| 88 | if(defined('REVISIONIZE_ROOT')) { |
| 89 | add_action('revisionize_after_create_revision', array($this, 'revisionize_keep_post_uri'), 9, 2); |
| 90 | add_action('revisionize_before_publish', array($this,'revisionize_clone_uri'), 9, 2); |
| 91 | } |
| 92 | |
| 93 | // 10. WP All Import |
| 94 | if(class_exists('PMXI_Plugin') && (!empty($permalink_manager_options['general']['pmxi_support']))) { |
| 95 | add_action('pmxi_extend_options_featured', array($this, 'wpaiextra_uri_display'), 9, 2); |
| 96 | add_filter('pmxi_options_options', array($this, 'wpai_api_options')); |
| 97 | add_filter('pmxi_addons', array($this, 'wpai_api_register')); |
| 98 | add_filter('wp_all_import_addon_parse', array($this, 'wpai_api_parse')); |
| 99 | add_filter('wp_all_import_addon_import', array($this, 'wpai_api_import')); |
| 100 | |
| 101 | add_action('pmxi_saved_post', array($this, 'wpai_save_redirects')); |
| 102 | |
| 103 | add_action('pmxi_after_xml_import', array($this, 'wpai_schedule_regenerate_uris_after_xml_import'), 10, 1); |
| 104 | add_action('wpai_regenerate_uris_after_import_event', array($this, 'wpai_regenerate_uris_after_import'), 10, 1); |
| 105 | } |
| 106 | |
| 107 | // 11. Duplicate Post |
| 108 | if(defined('DUPLICATE_POST_CURRENT_VERSION')) { |
| 109 | add_action('dp_duplicate_post', array($this, 'duplicate_custom_uri'), 10, 2); |
| 110 | add_action('dp_duplicate_page', array($this, 'duplicate_custom_uri'), 10, 2); |
| 111 | } |
| 112 | |
| 113 | // 12. My Listing by 27collective |
| 114 | if(class_exists('\MyListing\Post_Types')) { |
| 115 | add_filter('permalink_manager_filter_default_post_uri', array($this, 'ml_listing_custom_fields'), 5, 5 ); |
| 116 | add_action('mylisting/submission/save-listing-data', array($this, 'ml_set_listing_uri'), 100); |
| 117 | add_filter('permalink_manager_filter_query', array($this, 'ml_detect_archives'), 1); |
| 118 | } |
| 119 | |
| 120 | // 13. bbPress |
| 121 | if(class_exists('bbPress') && function_exists('bbp_get_edit_slug')) { |
| 122 | add_filter('permalink_manager_endpoints', array($this, 'bbpress_endpoints'), 9); |
| 123 | add_action('wp', array($this, 'bbpress_detect_endpoints'), 0); |
| 124 | } |
| 125 | |
| 126 | // 14. Dokan |
| 127 | if(class_exists('WeDevs_Dokan')) { |
| 128 | add_action('wp', array($this, 'dokan_detect_endpoints'), 999); |
| 129 | add_filter('permalink_manager_endpoints', array($this,'dokan_endpoints')); |
| 130 | } |
| 131 | |
| 132 | // 15. GeoDirectory |
| 133 | if(class_exists('GeoDirectory')) { |
| 134 | add_filter('permalink_manager_filter_default_post_uri', array($this, 'geodir_custom_fields'), 5, 5 ); |
| 135 | } |
| 136 | |
| 137 | // 16. BasePress |
| 138 | if(class_exists('Basepress')) { |
| 139 | add_filter('permalink_manager_filter_query', array($this, 'kb_adjust_query'), 5, 5); |
| 140 | } |
| 141 | |
| 142 | // 17. Ultimate Member |
| 143 | if(class_exists('UM') && !(empty($permalink_manager_options['general']['um_support']))) { |
| 144 | add_filter('permalink_manager_detect_uri', array($this, 'um_detect_extra_pages'), 20); |
| 145 | } |
| 146 | |
| 147 | // 18. WooCommerce Subscriptions |
| 148 | if(class_exists('WC_Subscriptions')) { |
| 149 | add_filter('permalink_manager_filter_final_post_permalink', array($this, 'fix_wcs_subscription_links'), 10, 3); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Some of the hooks must be called shortly after all the plugins are loaded |
| 155 | */ |
| 156 | public function init_early_hooks() { |
| 157 | // WP Store Locator |
| 158 | if(class_exists('WPSL_CSV')) { |
| 159 | add_action('added_post_meta', array($this, 'wpsl_regenerate_after_import'), 10, 4); |
| 160 | add_action('updated_post_meta', array($this, 'wpsl_regenerate_after_import'), 10, 4); |
| 161 | } |
| 162 | // Woocommerce |
| 163 | if(class_exists('WooCommerce')) { |
| 164 | add_filter('woocommerce_get_endpoint_url', array('Permalink_Manager_Core_Functions', 'control_trailing_slashes'), 9); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * 0. Stop redirect |
| 170 | */ |
| 171 | public static function stop_redirect() { |
| 172 | global $wp, $wp_query, $post; |
| 173 | |
| 174 | if(!empty($wp_query->query)) { |
| 175 | $query_vars = $wp_query->query; |
| 176 | |
| 177 | // WordPress Photo Seller Plugin |
| 178 | if(!empty($query_vars['image_id']) && !empty($query_vars['gallery_id'])) { |
| 179 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 180 | } |
| 181 | // Ultimate Member |
| 182 | else if(!empty($query_vars['um_user']) || !empty($query_vars['um_tab']) || (!empty($query_vars['provider']) && !empty($query_vars['state']))) { |
| 183 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 184 | } |
| 185 | // Mailster |
| 186 | else if(!empty($query_vars['_mailster_page'])) { |
| 187 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 188 | } |
| 189 | // WP Route |
| 190 | else if(!empty($query_vars['WP_Route'])) { |
| 191 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 192 | } |
| 193 | // WooCommerce Wishlist |
| 194 | else if(!empty($query_vars['wishlist-action'])) { |
| 195 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 196 | } |
| 197 | // UserPro |
| 198 | else if(!empty($query_vars['up_username'])) { |
| 199 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 200 | } |
| 201 | // The Events Calendar |
| 202 | else if(!empty($query_vars['eventDisplay'])) { |
| 203 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 204 | } |
| 205 | // Groundhogg |
| 206 | else if(class_exists('\Groundhogg\Plugin') && function_exists('\Groundhogg\get_managed_page_name')) { |
| 207 | $gh_slug = \Groundhogg\get_managed_page_name(); |
| 208 | |
| 209 | if(!empty($gh_slug) && !empty($wp->request) && (strpos($wp->request, $gh_slug) === 0)) { |
| 210 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 211 | } |
| 212 | } |
| 213 | // MyListing theme |
| 214 | else if(!empty($query_vars['explore_tab']) || !empty($query_vars['explore_region']) || !empty($_POST['submit_job'])) { |
| 215 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 216 | } |
| 217 | // GeoDirectory |
| 218 | else if(function_exists('geodir_location_page_id') && !empty($post->ID) && geodir_location_page_id() == $post->ID) { |
| 219 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 220 | } |
| 221 | // RankMath Pro |
| 222 | else if(isset($query_vars['schema-preview'])) { |
| 223 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // WPForo |
| 228 | if(class_exists('wpForo')) { |
| 229 | $forum_page_id = get_option('wpforo_pageid'); |
| 230 | |
| 231 | if(!empty($forum_page_id) && !empty($post->ID) && $forum_page_id == $post->ID) { |
| 232 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * 2. AMP hooks |
| 239 | */ |
| 240 | function detect_amp($uri_parts, $request_url) { |
| 241 | global $amp_enabled; |
| 242 | $amp_query_var = AMP_QUERY_VAR; |
| 243 | |
| 244 | // Check if AMP should be triggered |
| 245 | preg_match("/^(.+?)\/({$amp_query_var})?\/?$/i", $uri_parts['uri'], $regex_parts); |
| 246 | if(!empty($regex_parts[2])) { |
| 247 | $uri_parts['uri'] = $regex_parts[1]; |
| 248 | $amp_enabled = true; |
| 249 | } |
| 250 | |
| 251 | return $uri_parts; |
| 252 | } |
| 253 | |
| 254 | function enable_amp($query) { |
| 255 | global $amp_enabled; |
| 256 | |
| 257 | if(!empty($amp_enabled)) { |
| 258 | $query[AMP_QUERY_VAR] = 1; |
| 259 | } |
| 260 | |
| 261 | return $query; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * 3. Parse Custom Permalinks import |
| 266 | */ |
| 267 | public static function custom_permalinks_uris() { |
| 268 | global $wpdb; |
| 269 | |
| 270 | $custom_permalinks_uris = array(); |
| 271 | |
| 272 | // 1. List tags/categories |
| 273 | $table = get_option('custom_permalink_table'); |
| 274 | if($table && is_array($table)) { |
| 275 | foreach ( $table as $permalink => $info ) { |
| 276 | $custom_permalinks_uris[] = array( |
| 277 | 'id' => "tax-" . $info['id'], |
| 278 | 'uri' => trim($permalink, "/") |
| 279 | ); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // 2. List posts/pages |
| 284 | $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 != '';"; |
| 285 | $posts = $wpdb->get_results($query); |
| 286 | foreach($posts as $post) { |
| 287 | $custom_permalinks_uris[] = array( |
| 288 | 'id' => $post->ID, |
| 289 | 'uri' => trim($post->meta_value, "/"), |
| 290 | ); |
| 291 | } |
| 292 | |
| 293 | return $custom_permalinks_uris; |
| 294 | } |
| 295 | |
| 296 | static public function import_custom_permalinks_uris() { |
| 297 | global $permalink_manager_uris, $permalink_manager_before_sections_html; |
| 298 | |
| 299 | $custom_permalinks_plugin = 'custom-permalinks/custom-permalinks.php'; |
| 300 | |
| 301 | if(is_plugin_active($custom_permalinks_plugin) && !empty($_POST['disable_custom_permalinks'])) { |
| 302 | deactivate_plugins($custom_permalinks_plugin); |
| 303 | } |
| 304 | |
| 305 | // Get a list of imported URIs |
| 306 | $custom_permalinks_uris = self::custom_permalinks_uris(); |
| 307 | |
| 308 | if(!empty($custom_permalinks_uris) && count($custom_permalinks_uris) > 0) { |
| 309 | foreach($custom_permalinks_uris as $item) { |
| 310 | $permalink_manager_uris[$item['id']] = Permalink_Manager_Helper_Functions::sanitize_title($item['uri']); |
| 311 | } |
| 312 | |
| 313 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( '"Custom Permalinks" URIs were imported!', 'permalink-manager' ), 'updated'); |
| 314 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 315 | } else { |
| 316 | $permalink_manager_before_sections_html .= Permalink_Manager_Admin_Functions::get_alert_message(__( 'No "Custom Permalinks" URIs were imported!', 'permalink-manager' ), 'error'); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * 4. WooCommerce |
| 322 | */ |
| 323 | function woocommerce_detect($query) { |
| 324 | global $woocommerce, $pm_query; |
| 325 | |
| 326 | $shop_page_id = get_option('woocommerce_shop_page_id'); |
| 327 | |
| 328 | // WPML - translate shop page id |
| 329 | $shop_page_id = apply_filters('wpml_object_id', $shop_page_id, 'page', TRUE); |
| 330 | |
| 331 | // Fix shop page |
| 332 | if(get_theme_support('woocommerce') && !empty($pm_query['id']) && is_numeric($pm_query['id']) && $shop_page_id == $pm_query['id']) { |
| 333 | $query['post_type'] = 'product'; |
| 334 | unset($query['pagename']); |
| 335 | } |
| 336 | |
| 337 | // Fix WooCommerce pages |
| 338 | if(!empty($woocommerce->query->query_vars)) { |
| 339 | $query_vars = $woocommerce->query->query_vars; |
| 340 | |
| 341 | foreach($query_vars as $key => $val) { |
| 342 | if(isset($query[$key])) { |
| 343 | $woocommerce_page = true; |
| 344 | $query['do_not_redirect'] = 1; |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return $query; |
| 351 | } |
| 352 | |
| 353 | function woocommerce_checkout_fix() { |
| 354 | global $wp_query, $pm_query, $permalink_manager_options; |
| 355 | |
| 356 | // Redirect from Shop archive to selected page |
| 357 | if(is_shop() && empty($pm_query['id'])) { |
| 358 | $redirect_mode = (!empty($permalink_manager_options['general']['redirect'])) ? $permalink_manager_options['general']['redirect'] : false; |
| 359 | $redirect_shop = apply_filters('permalink_manager_redirect_shop_archive', false); |
| 360 | $shop_page = get_option('woocommerce_shop_page_id'); |
| 361 | |
| 362 | if($redirect_mode && $redirect_shop && $shop_page && empty($wp_query->query_vars['s'])) { |
| 363 | $shop_url = get_permalink($shop_page); |
| 364 | wp_safe_redirect($shop_url, $redirect_mode); |
| 365 | exit(); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // Do not redirect "thank you" & another WooCommerce pages |
| 370 | if(is_checkout() || (function_exists('is_wc_endpoint_url') && is_wc_endpoint_url())) { |
| 371 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | function woocommerce_generate_permalinks_after_duplicate($new_product, $old_product) { |
| 376 | global $permalink_manager_uris; |
| 377 | |
| 378 | if(!empty($new_product)) { |
| 379 | $product_id = $new_product->get_id(); |
| 380 | |
| 381 | // Ignore variations |
| 382 | if($new_product->get_type() !== 'variation') { |
| 383 | $custom_uri = Permalink_Manager_URI_Functions_Post::get_default_post_uri($product_id, false, true); |
| 384 | |
| 385 | Permalink_Manager_URI_Functions::save_single_uri($product_id, $custom_uri, false, true); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | function woocommerce_product_attributes($default_uri, $slug, $post, $post_name, $native_uri) { |
| 391 | // Do not affect native URIs |
| 392 | if($native_uri == true) { return $default_uri; } |
| 393 | |
| 394 | // Use only for products |
| 395 | if(empty($post->post_type) || $post->post_type !== 'product') { return $default_uri; } |
| 396 | |
| 397 | preg_match_all("/%pa_(.[^\%]+)%/", $default_uri, $custom_fields); |
| 398 | |
| 399 | if(!empty($custom_fields[1])) { |
| 400 | $product = wc_get_product($post->ID); |
| 401 | |
| 402 | foreach($custom_fields[1] as $i => $custom_field) { |
| 403 | $attribute_name = sanitize_title($custom_field); |
| 404 | $attribute_value = $product->get_attribute($attribute_name); |
| 405 | |
| 406 | $default_uri = str_replace($custom_fields[0][$i], Permalink_Manager_Helper_Functions::sanitize_title($attribute_value), $default_uri); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | return $default_uri; |
| 411 | } |
| 412 | |
| 413 | function woocommerce_translate_ajax_fragments_urls($permalink, $post, $old_permalink) { |
| 414 | // Use it only if the permalinks are different |
| 415 | if($permalink == $old_permalink || $post->post_type !== 'page') { |
| 416 | return $permalink; |
| 417 | } |
| 418 | |
| 419 | // A. Native WooCommerce AJAX events |
| 420 | if(!empty($_REQUEST['wc-ajax'])) { |
| 421 | $action = sanitize_title($_REQUEST['wc-ajax']); |
| 422 | } |
| 423 | // B. Shoptimizer theme |
| 424 | else if(!empty($_REQUEST['action'])) { |
| 425 | $action = sanitize_title($_REQUEST['action']); |
| 426 | } |
| 427 | |
| 428 | // Allowed action names |
| 429 | $allowed_actions = array('shoptimizer_pdp_ajax_atc', 'get_refreshed_fragments'); |
| 430 | |
| 431 | if(in_array($action, $allowed_actions)) { |
| 432 | $translated_post_id = apply_filters('wpml_object_id', $post->ID, 'page'); |
| 433 | $permalink = ($translated_post_id !== $post->ID) ? get_permalink($translated_post_id) : $permalink; |
| 434 | } |
| 435 | |
| 436 | return $permalink; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * 4A. WooCommerce CSV Import/Export |
| 441 | */ |
| 442 | function woocommerce_csv_custom_uri_column($columns) { |
| 443 | if(!is_array($columns)) { return $columns; } |
| 444 | |
| 445 | $label = __('Custom URI', 'permalink-manager'); |
| 446 | $key = 'custom_uri'; |
| 447 | |
| 448 | if(current_filter() == 'woocommerce_csv_product_import_mapping_default_columns') { |
| 449 | $columns[$label] = $key; |
| 450 | } else { |
| 451 | $columns[$key] = $label; |
| 452 | } |
| 453 | |
| 454 | return $columns; |
| 455 | } |
| 456 | |
| 457 | function woocommerce_export_custom_uri_value($value, $product, $column_id) { |
| 458 | if(empty($value) && !empty($product)) { |
| 459 | $product_id = $product->get_id(); |
| 460 | |
| 461 | // Get custom permalink or default permalink |
| 462 | $value = Permalink_Manager_URI_Functions_Post::get_post_uri($product_id); |
| 463 | } |
| 464 | |
| 465 | return $value; |
| 466 | } |
| 467 | |
| 468 | function woocommerce_csv_import_custom_uri($product, $data) { |
| 469 | global $permalink_manager_uris; |
| 470 | |
| 471 | if(!empty($product)) { |
| 472 | $product_id = $product->get_id(); |
| 473 | |
| 474 | // Ignore variations |
| 475 | if($product->get_type() == 'variation') { |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | // A. Use default permalink if "Custom URI" is not set and did not exist before |
| 480 | if(empty($permalink_manager_uris[$product_id]) && empty($data['custom_uri'])) { |
| 481 | $custom_uri = Permalink_Manager_URI_Functions_Post::get_default_post_uri($product_id, false, true); |
| 482 | } else if(!empty($data['custom_uri'])) { |
| 483 | $custom_uri = Permalink_Manager_Helper_Functions::sanitize_title($data['custom_uri']); |
| 484 | } else { |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | Permalink_Manager_URI_Functions::save_single_uri($product_id, $custom_uri, false, true); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * 5. Theme My Login |
| 494 | */ |
| 495 | function tml_keep_query_parameters($permalink, $post, $old_permalink) { |
| 496 | // Get the query string from old permalink |
| 497 | $get_parameters = (($pos = strpos($old_permalink, "?")) !== false) ? substr($old_permalink, $pos) : ""; |
| 498 | |
| 499 | return $permalink . $get_parameters; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * 6. Yoast SEO hooks |
| 504 | */ |
| 505 | function yoast_fix_sitemap_urls($permalink) { |
| 506 | if(class_exists('WPSEO_Utils')) { |
| 507 | $home_url = WPSEO_Utils::home_url(); |
| 508 | $home_protocol = parse_url($home_url, PHP_URL_SCHEME); |
| 509 | |
| 510 | $permalink = preg_replace("/^http(s)?/", $home_protocol, $permalink); |
| 511 | } |
| 512 | |
| 513 | return $permalink; |
| 514 | } |
| 515 | |
| 516 | function yoast_update_indexable_permalink($element_id, $new_uri, $old_uri) { |
| 517 | global $wpdb; |
| 518 | |
| 519 | if(!empty($new_uri) && !empty($old_uri) && $new_uri !== $old_uri) { |
| 520 | if(current_filter() == 'permalink_manager_updated_term_uri') { |
| 521 | $permalink = get_term_link((int) $element_id); |
| 522 | $object_type = 'term'; |
| 523 | } else { |
| 524 | $permalink = get_permalink($element_id); |
| 525 | $object_type = 'post'; |
| 526 | } |
| 527 | |
| 528 | if(!empty($permalink)) { |
| 529 | $permalink_hash = strlen($permalink) . ':' . md5($permalink); |
| 530 | $wpdb->update("{$wpdb->prefix}yoast_indexable", array('permalink' => $permalink, 'permalink_hash' => $permalink_hash), array('object_id' => $element_id, 'object_type' => $object_type), array('%s', '%s'), array('%d', '%s')); |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | function yoast_fix_canonical($url) { |
| 536 | global $pm_query, $wp_rewrite; |
| 537 | |
| 538 | if(!empty($pm_query['id'])) { |
| 539 | $element = get_queried_object(); |
| 540 | |
| 541 | if(!empty($element->ID) && !empty($element->post_type)) { |
| 542 | $new_url = get_permalink($element->ID); |
| 543 | |
| 544 | // Do not filter if custom canonical URL is set |
| 545 | $yoast_canonical_url = get_post_meta($element->ID, '_yoast_wpseo_canonical', true); |
| 546 | if(!empty($yoast_canonical_url)) { return $url; } |
| 547 | |
| 548 | $paged = (get_query_var('page')) ? get_query_var('page') : 1; |
| 549 | if($paged > 1) { |
| 550 | $new_url = sprintf('%s/%d', trim($new_url, '/'), $paged); |
| 551 | } |
| 552 | } else if(!empty($element->taxonomy) && !empty($element->term_id)) { |
| 553 | $new_url = get_term_link($element, $element->taxonomy); |
| 554 | |
| 555 | // Do not filter if custom canonical URL is set |
| 556 | if(class_exists('WPSEO_Taxonomy_Meta')) { |
| 557 | $yoast_canonical_url = WPSEO_Taxonomy_Meta::get_term_meta($element, $element->taxonomy, 'canonical' ); |
| 558 | if(!empty($yoast_canonical_url)) { return $url; } |
| 559 | } |
| 560 | |
| 561 | $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; |
| 562 | if($paged > 1) { |
| 563 | $new_url = sprintf('%s/%s/%d', trim($new_url, '/'), $wp_rewrite->pagination_base, $paged); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | $url = (!empty($new_url)) ? $new_url : $url; |
| 568 | $url = Permalink_Manager_Core_Functions::control_trailing_slashes($url); |
| 569 | } |
| 570 | |
| 571 | return $url; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * 7. Breadcrumbs |
| 576 | */ |
| 577 | function filter_breadcrumbs($links) { |
| 578 | // Get post type permastructure settings |
| 579 | global $permalink_manager_uris, $permalink_manager_options, $post, $wpdb, $wp, $wp_current_filter; |
| 580 | |
| 581 | // Check if the filter should be activated |
| 582 | if(empty($permalink_manager_options['general']['yoast_breadcrumbs']) || empty($permalink_manager_uris)) { return $links; } |
| 583 | |
| 584 | // Get current post/page/term (if available) |
| 585 | $queried_element = get_queried_object(); |
| 586 | if(!empty($queried_element->ID)) { |
| 587 | $element_id = $queried_element->ID; |
| 588 | } else if(!empty($queried_element->term_id)) { |
| 589 | $element_id = "tax-{$queried_element->term_id}"; |
| 590 | } |
| 591 | |
| 592 | // Get the custom permalink (if available) or the current request URL (if unavailable) |
| 593 | if(!empty($element_id) && !empty($permalink_manager_uris[$element_id])) { |
| 594 | $custom_uri = preg_replace("/([^\/]+)$/", '', $permalink_manager_uris[$element_id]); |
| 595 | } else { |
| 596 | $custom_uri = trim(preg_replace("/([^\/]+)$/", '', $wp->request), "/"); |
| 597 | } |
| 598 | |
| 599 | $all_uris = array_flip($permalink_manager_uris); |
| 600 | $custom_uri_parts = explode('/', trim($custom_uri)); |
| 601 | $breadcrumbs = array(); |
| 602 | $snowball = ''; |
| 603 | $available_taxonomies = Permalink_Manager_Helper_Functions::get_taxonomies_array(); |
| 604 | $available_post_types = Permalink_Manager_Helper_Functions::get_post_types_array(); |
| 605 | $current_filter = end($wp_current_filter); |
| 606 | |
| 607 | // Get Yoast Meta (the breadcrumbs titles can be changed in Yoast metabox) |
| 608 | $yoast_meta_terms = get_option('wpseo_taxonomy_meta'); |
| 609 | |
| 610 | // Get internal breadcrumb elements |
| 611 | foreach($custom_uri_parts as $slug) { |
| 612 | if(empty($slug)) { continue; } |
| 613 | |
| 614 | $snowball = (empty($snowball)) ? $slug : "{$snowball}/{$slug}"; |
| 615 | |
| 616 | // 1A. Try to match any custom URI |
| 617 | if($snowball) { |
| 618 | $uri = trim($snowball, "/"); |
| 619 | $element = (!empty($all_uris[$uri])) ? $all_uris[$uri] : false; |
| 620 | |
| 621 | if(!empty($element) && strpos($element, 'tax-') !== false) { |
| 622 | $element_id = intval(preg_replace("/[^0-9]/", "", $element)); |
| 623 | $element = get_term($element_id); |
| 624 | } else if(is_numeric($element)) { |
| 625 | $element = get_post($element); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | // 1B. Try to get term |
| 630 | if(empty($element) && !empty($available_taxonomies)) { |
| 631 | $sql = sprintf("SELECT t.term_id, t.name, tt.taxonomy FROM {$wpdb->terms} AS t LEFT JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE slug = '%s' AND tt.taxonomy IN ('%s') LIMIT 1", esc_sql($slug), implode("','", array_keys($available_taxonomies))); |
| 632 | |
| 633 | $element = $wpdb->get_row($sql); |
| 634 | } |
| 635 | |
| 636 | // 1C. Try to get page/post |
| 637 | if(empty($element) && !empty($available_post_types)) { |
| 638 | $sql = sprintf("SELECT ID, post_title, post_type FROM {$wpdb->posts} WHERE post_name = '%s' AND post_status = 'publish' AND post_type IN ('%s') AND post_type != 'attachment' LIMIT 1", esc_sql($slug), implode("','", array_keys($available_post_types))); |
| 639 | |
| 640 | $element = $wpdb->get_row($sql); |
| 641 | } |
| 642 | |
| 643 | // 2A. When the term is found, we can add it to the breadcrumbs |
| 644 | if(!empty($element->term_id)) { |
| 645 | $term_id = apply_filters('wpml_object_id', $element->term_id, $element->taxonomy, true); |
| 646 | $term = ($element->term_id !== $term_id) ? get_term($term_id) : $element; |
| 647 | |
| 648 | $title = (!empty($yoast_meta_terms[$term->taxonomy][$term->term_id]['wpseo_bctitle'])) ? $yoast_meta_terms[$term->taxonomy][$term->term_id]['wpseo_bctitle'] : $term->name; |
| 649 | |
| 650 | $breadcrumbs[] = array( |
| 651 | 'text' => $title, |
| 652 | 'url' => get_term_link((int) $term->term_id, $term->taxonomy), |
| 653 | ); |
| 654 | } |
| 655 | // 2B. When the post/page is found, we can add it to the breadcrumbs |
| 656 | else if(!empty($element->ID)) { |
| 657 | $page_id = apply_filters('wpml_object_id', $element->ID, $element->post_type, true); |
| 658 | $page = ($element->ID !== $page_id) ? get_post($page_id) : $element; |
| 659 | |
| 660 | $title = get_post_meta($page->ID, '_yoast_wpseo_bctitle', true); |
| 661 | $title = (!empty($title)) ? $title : $page->post_title; |
| 662 | |
| 663 | $breadcrumbs[] = array( |
| 664 | 'text' => $title, |
| 665 | 'url' => get_permalink($page->ID), |
| 666 | ); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | // Add new links to current breadcrumbs array |
| 671 | if(!empty($links) && is_array($links)) { |
| 672 | $first_element = reset($links); |
| 673 | $last_element = end($links); |
| 674 | $breadcrumbs = (!empty($breadcrumbs)) ? $breadcrumbs : array(); |
| 675 | |
| 676 | // Support RankMath/SEOPress/WooCommerce/Slim SEO breadcrumbs |
| 677 | if(in_array($current_filter, array('wpseo_breadcrumb_links', 'rank_math/frontend/breadcrumb/items', 'seopress_pro_breadcrumbs_crumbs', 'woocommerce_get_breadcrumb', 'slim_seo_breadcrumbs_links'))) { |
| 678 | foreach($breadcrumbs as &$breadcrumb) { |
| 679 | if(isset($breadcrumb['text'])) { |
| 680 | $breadcrumb[0] = $breadcrumb['text']; |
| 681 | $breadcrumb[1] = $breadcrumb['url']; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | if(in_array($current_filter, array('slim_seo_breadcrumbs_links'))) { |
| 687 | $links = array_merge(array($first_element), $breadcrumbs); |
| 688 | } else { |
| 689 | $links = array_merge(array($first_element), $breadcrumbs, array($last_element)); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | return array_filter($links); |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * 8. Support WooCommerce Wishlist Plugin |
| 698 | */ |
| 699 | function ti_woocommerce_wishlist_uris($uri_parts, $request_url, $endpoints) { |
| 700 | global $permalink_manager_uris, $wp; |
| 701 | |
| 702 | $wishlist_pid = tinv_get_option('general', 'page_wishlist'); |
| 703 | |
| 704 | // Find the Wishlist page URI |
| 705 | if(is_numeric($wishlist_pid) && !empty($permalink_manager_uris[$wishlist_pid])) { |
| 706 | $wishlist_uri = preg_quote($permalink_manager_uris[$wishlist_pid], '/'); |
| 707 | |
| 708 | // Extract the Wishlist ID |
| 709 | preg_match("/^({$wishlist_uri})\/([^\/]+)\/?$/", $uri_parts['uri'], $output_array); |
| 710 | |
| 711 | if(!empty($output_array[2])) { |
| 712 | $uri_parts['uri'] = $output_array[1]; |
| 713 | $uri_parts['endpoint'] = 'tinvwlID'; |
| 714 | $uri_parts['endpoint_value'] = $output_array[2]; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | return $uri_parts; |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * 9. Revisionize |
| 723 | */ |
| 724 | function revisionize_keep_post_uri($old_id, $new_id) { |
| 725 | global $permalink_manager_uris; |
| 726 | |
| 727 | // Copy the custom URI from original post and apply it to the new temp. revision post |
| 728 | if(!empty($permalink_manager_uris[$old_id])) { |
| 729 | $permalink_manager_uris[$new_id] = $permalink_manager_uris[$old_id]; |
| 730 | |
| 731 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | function revisionize_clone_uri($old_id, $new_id) { |
| 736 | global $permalink_manager_uris; |
| 737 | |
| 738 | if(!empty($permalink_manager_uris[$new_id])) { |
| 739 | // Copy the custom URI from revision post and apply it to the original post |
| 740 | $permalink_manager_uris[$old_id] = $permalink_manager_uris[$new_id]; |
| 741 | unset($permalink_manager_uris[$new_id]); |
| 742 | |
| 743 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * 10. WP All Import |
| 749 | */ |
| 750 | function wpaiextra_uri_display($content_type, $current_values) { |
| 751 | // Check if post type is supported |
| 752 | if($content_type !== 'taxonomies' && Permalink_Manager_Helper_Functions::is_disabled($content_type)) { |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | // Get custom URI format |
| 757 | $custom_uri = (!empty($current_values['custom_uri'])) ? sanitize_text_field($current_values['custom_uri']) : ""; |
| 758 | |
| 759 | $html = '<div class="wpallimport-collapsed closed wpallimport-section">'; |
| 760 | $html .= '<div class="wpallimport-content-section">'; |
| 761 | $html .= sprintf('<div class="wpallimport-collapsed-header"><h3>%s</h3></div>', __('Permalink Manager', 'permalink-manager')); |
| 762 | $html .= '<div class="wpallimport-collapsed-content">'; |
| 763 | |
| 764 | $html .= '<div class="template_input">'; |
| 765 | $html .= Permalink_Manager_Admin_Functions::generate_option_field('custom_uri', array('extra_atts' => 'style="width:100%; line-height: 25px;"', 'placeholder' => __('Custom URI', 'permalink-manager'), 'value' => $custom_uri)); |
| 766 | $html .= wpautop(sprintf(__('If empty, a default permalink based on your current <a href="%s" target="_blank">permastructure settings</a> will be used.', 'permalink-manager'), Permalink_Manager_Admin_Functions::get_admin_url('§ion=permastructs'))); |
| 767 | $html .= '</div>'; |
| 768 | |
| 769 | // $html .= print_r($current_values, true); |
| 770 | |
| 771 | $html .= '</div>'; |
| 772 | $html .= '</div>'; |
| 773 | $html .= '</div>'; |
| 774 | |
| 775 | echo $html; |
| 776 | } |
| 777 | |
| 778 | function wpai_api_options($all_options) { |
| 779 | return $all_options + array('custom_uri' => null); |
| 780 | } |
| 781 | |
| 782 | function wpai_api_register($addons) { |
| 783 | if(empty($addons[PERMALINK_MANAGER_PLUGIN_SLUG])) { |
| 784 | $addons[PERMALINK_MANAGER_PLUGIN_SLUG] = 1; |
| 785 | } |
| 786 | return $addons; |
| 787 | } |
| 788 | |
| 789 | function wpai_api_parse($functions) { |
| 790 | $functions[PERMALINK_MANAGER_PLUGIN_SLUG] = array($this, 'wpai_api_parse_function'); |
| 791 | return $functions; |
| 792 | } |
| 793 | |
| 794 | function wpai_api_import($functions) { |
| 795 | $functions[PERMALINK_MANAGER_PLUGIN_SLUG] = array($this, 'wpai_api_import_function'); |
| 796 | return $functions; |
| 797 | } |
| 798 | |
| 799 | function wpai_api_parse_function($data) { |
| 800 | extract($data); |
| 801 | |
| 802 | $data = array(); // parsed data |
| 803 | $option_name = 'custom_uri'; |
| 804 | |
| 805 | if(!empty($import->options[$option_name])) { |
| 806 | $this->logger = $data['logger']; |
| 807 | $cxpath = $xpath_prefix . $import->xpath; |
| 808 | $tmp_files = array(); |
| 809 | |
| 810 | if(isset($import->options[$option_name]) && $import->options[$option_name] != '') { |
| 811 | if($import->options[$option_name] == "xpath") { |
| 812 | if ($import->options[$this->slug]['xpaths'][$option_name] == "") { |
| 813 | $count and $this->data[$option_name] = array_fill(0, $count, ""); |
| 814 | } else { |
| 815 | $data[$option_name] = XmlImportParser::factory($xml, $cxpath, (string) $import->options['xpaths'][$option_name], $file)->parse(); |
| 816 | $tmp_files[] = $file; |
| 817 | } |
| 818 | } else { |
| 819 | $data[$option_name] = XmlImportParser::factory($xml, $cxpath, (string) $import->options[$option_name], $file)->parse(); |
| 820 | $tmp_files[] = $file; |
| 821 | } |
| 822 | } else { |
| 823 | $data[$option_name] = array_fill(0, $count, ""); |
| 824 | } |
| 825 | |
| 826 | foreach ($tmp_files as $file) { |
| 827 | unlink($file); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | return $data; |
| 832 | } |
| 833 | |
| 834 | function wpai_api_import_function($importData, $parsedData) { |
| 835 | global $permalink_manager_uris; |
| 836 | |
| 837 | // Check if the array with $parsedData is not empty |
| 838 | if(empty($parsedData) || empty($importData['post_type'])) { return; } |
| 839 | |
| 840 | // Check if the imported elements are terms |
| 841 | if($importData['post_type'] == 'taxonomies') { |
| 842 | $is_term = true; |
| 843 | } else if(Permalink_Manager_Helper_Functions::is_disabled($importData['post_type'], 'post_type')) { |
| 844 | return; |
| 845 | } |
| 846 | |
| 847 | // Get the parsed custom URI |
| 848 | $index = (isset($importData['i'])) ? $importData['i'] : false; |
| 849 | $pid = (!empty($importData['pid'])) ? $importData['pid'] : false; |
| 850 | |
| 851 | // Prepend "tax-" prefix if needed |
| 852 | $pid = (!empty($is_term) && !empty($pid)) ? "tax-{$pid}" : $pid; |
| 853 | |
| 854 | if(isset($index) && !empty($pid) && !empty($parsedData['custom_uri'][$index])) { |
| 855 | $custom_uri = Permalink_Manager_Helper_Functions::sanitize_title($parsedData['custom_uri'][$index]); |
| 856 | |
| 857 | if(!empty($custom_uri)) { |
| 858 | $permalink_manager_uris[$pid] = $custom_uri; |
| 859 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | function wpai_save_redirects($pid) { |
| 865 | global $permalink_manager_external_redirects, $permalink_manager_uris; |
| 866 | |
| 867 | $external_url = get_post_meta($pid, '_external_redirect', true); |
| 868 | $external_url = (empty($external_url)) ? get_post_meta($pid, 'external_redirect', true) : $external_url; |
| 869 | |
| 870 | if($external_url && class_exists('Permalink_Manager_Pro_Functions')) { |
| 871 | Permalink_Manager_Pro_Functions::save_external_redirect($external_url, $pid); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | function wpai_schedule_regenerate_uris_after_xml_import($import_id) { |
| 876 | global $wpdb; |
| 877 | |
| 878 | $post_ids = $wpdb->get_col("SELECT post_id FROM {$wpdb->prefix}pmxi_posts WHERE import_id = {$import_id}"); |
| 879 | $chunks = array_chunk($post_ids, 200); |
| 880 | |
| 881 | // Schedule URI regenerate and split into bulks |
| 882 | foreach($chunks as $i => $chunk) { |
| 883 | wp_schedule_single_event(time() + ($i * 30), 'wpai_regenerate_uris_after_import_event', array($chunk)); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | function wpai_regenerate_uris_after_import($post_ids) { |
| 888 | global $permalink_manager_uris; |
| 889 | |
| 890 | if(!is_array($post_ids)) { return; } |
| 891 | |
| 892 | foreach($post_ids as $id) { |
| 893 | if(!empty($permalink_manager_uris[$id])) { continue; } |
| 894 | $permalink_manager_uris[$id] = Permalink_Manager_URI_Functions_Post::get_default_post_uri($id); |
| 895 | } |
| 896 | |
| 897 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * 11. Duplicate Page |
| 902 | */ |
| 903 | function duplicate_custom_uri($new_post_id, $old_post) { |
| 904 | global $permalink_manager_uris; |
| 905 | |
| 906 | $duplicate_post_blacklist = get_option('duplicate_post_blacklist', false); |
| 907 | $duplicate_custom_uri_bool = (!empty($duplicate_post_blacklist) && strpos($duplicate_post_blacklist, 'custom_uri') !== false) ? false : true; |
| 908 | |
| 909 | if(!empty($old_post->ID) && $duplicate_custom_uri_bool) { |
| 910 | $old_post_id = intval($old_post->ID); |
| 911 | |
| 912 | // Clone custom permalink (if set for cloned post/page) |
| 913 | if(!empty($permalink_manager_uris[$old_post_id])) { |
| 914 | $old_post_uri = $permalink_manager_uris[$old_post_id]; |
| 915 | $new_post_uri = preg_replace('/(.+?)(\.[^\.]+$|$)/', '$1-2$2', $old_post_uri); |
| 916 | |
| 917 | $permalink_manager_uris[$new_post_id] = $new_post_uri; |
| 918 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * 12. My Listing by 27collective |
| 925 | */ |
| 926 | public function ml_listing_custom_fields($default_uri, $native_slug, $element, $slug, $native_uri) { |
| 927 | global $permalink_manager_uris; |
| 928 | |
| 929 | // Use only for "listing" post type & custom permalink |
| 930 | if(empty($element->post_type) || $element->post_type !== 'job_listing' || $native_uri) { return $default_uri; } |
| 931 | |
| 932 | // A1. Listing type |
| 933 | if(strpos($default_uri, '%listing-type%') !== false || strpos($default_uri, '%listing_type%') !== false) { |
| 934 | if(class_exists('MyListing\Src\Listing')) { |
| 935 | $listing_type_post = MyListing\Src\Listing::get($element); |
| 936 | $listing_type = (is_object($listing_type_post) && !empty($listing_type_post->type)) ? $listing_type_post->type->get_permalink_name() : ''; |
| 937 | } else { |
| 938 | $listing_type_slug = get_post_meta($element->ID, '_case27_listing_type', true); |
| 939 | $listing_type_post = get_page_by_path($listing_type_slug, OBJECT, 'case27_listing_type'); |
| 940 | |
| 941 | if(!empty($listing_type_post)) { |
| 942 | $listing_type_post_settings = get_post_meta($listing_type_post->ID, 'case27_listing_type_settings_page', true); |
| 943 | $listing_type_post_settings = (is_serialized($listing_type_post_settings)) ? unserialize($listing_type_post_settings) : array(); |
| 944 | |
| 945 | $listing_type = (!empty($listing_type_post_settings['permalink'])) ? $listing_type_post_settings['permalink'] : $listing_type_post->post_name; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | if(!empty($listing_type)) { |
| 950 | $default_uri = str_replace(array('%listing-type%', '%listing_type%'), Permalink_Manager_Helper_Functions::sanitize_title($listing_type, true), $default_uri); |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | // A2. Listing type (slug) |
| 955 | if(strpos($default_uri, '%listing-type-slug%') !== false || strpos($default_uri, '%listing_type_slug%') !== false || strpos($default_uri, '%case27_listing_type%') !== false) { |
| 956 | $listing_type = get_post_meta($element->ID, '_case27_listing_type', true); |
| 957 | |
| 958 | if(!empty($listing_type)) { |
| 959 | $listing_type = Permalink_Manager_Helper_Functions::sanitize_title($listing_type, true); |
| 960 | $default_uri = str_replace(array('%listing-type-slug%', '%listing_type_slug%', '%case27_listing_type%'), $listing_type, $default_uri); |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | // B. Listing location |
| 965 | if(strpos($default_uri, '%listing-location%') !== false || strpos($default_uri, '%listing_location%') !== false) { |
| 966 | $listing_location = get_post_meta($element->ID, '_job_location', true); |
| 967 | |
| 968 | if(!empty($listing_location)) { |
| 969 | $listing_location = Permalink_Manager_Helper_Functions::sanitize_title($listing_location, true); |
| 970 | $default_uri = str_replace(array('%listing-location%', '%listing_location%'), $listing_location, $default_uri); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | // C. Listing region |
| 975 | if(strpos($default_uri, '%listing-region%') !== false || strpos($default_uri, '%listing_region%') !== false) { |
| 976 | $listing_region_terms = wp_get_object_terms($element->ID, 'region'); |
| 977 | $listing_region_term = (!is_wp_error($listing_region_terms) && !empty($listing_region_terms) && is_object($listing_region_terms[0])) ? Permalink_Manager_Helper_Functions::get_lowest_element($listing_region_terms[0], $listing_region_terms) : ""; |
| 978 | |
| 979 | if(!empty($listing_region_term)) { |
| 980 | $listing_region = Permalink_Manager_Helper_Functions::get_term_full_slug($listing_region_term, $listing_region_terms, 2, false); |
| 981 | $listing_region = Permalink_Manager_Helper_Functions::sanitize_title($listing_region, true); |
| 982 | |
| 983 | $default_uri = str_replace(array('%listing-region%', '%listing_region%'), $listing_region, $default_uri); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | // D. Listing category |
| 988 | if(strpos($default_uri, '%listing-category%') !== false || strpos($default_uri, '%listing_category%') !== false) { |
| 989 | $listing_category_terms = wp_get_object_terms($element->ID, 'job_listing_category'); |
| 990 | $listing_category_term = (!is_wp_error($listing_category_terms) && !empty($listing_category_terms) && is_object($listing_category_terms[0])) ? Permalink_Manager_Helper_Functions::get_lowest_element($listing_category_terms[0], $listing_category_terms) : ""; |
| 991 | |
| 992 | if(!empty($listing_category_term)) { |
| 993 | $listing_category = Permalink_Manager_Helper_Functions::get_term_full_slug($listing_category_term, $listing_category_terms, 2, false); |
| 994 | $listing_category = Permalink_Manager_Helper_Functions::sanitize_title($listing_category, true); |
| 995 | |
| 996 | $default_uri = str_replace(array('%listing-category%', '%listing_category%'), $listing_category, $default_uri); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | return $default_uri; |
| 1001 | } |
| 1002 | |
| 1003 | function ml_set_listing_uri($post_id) { |
| 1004 | global $permalink_manager_uris; |
| 1005 | |
| 1006 | if(!empty($permalink_manager_uris)) { |
| 1007 | $default_uri = Permalink_Manager_URI_Functions_Post::get_default_post_uri($post_id); |
| 1008 | |
| 1009 | if($default_uri) { |
| 1010 | $permalink_manager_uris[$post_id] = $default_uri; |
| 1011 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | function ml_detect_archives($query) { |
| 1017 | if(function_exists('mylisting_custom_taxonomies') && empty($_POST['submit_job'])) { |
| 1018 | $explore_page_id = get_option('options_general_explore_listings_page', false); |
| 1019 | if(empty($explore_page_id)) { return $query; } |
| 1020 | |
| 1021 | $taxonomies = mylisting_custom_taxonomies(); |
| 1022 | $taxonomies = array_merge(array_keys($taxonomies), array('job_listing_category', 'region', 'case27_job_listing_tags')); |
| 1023 | |
| 1024 | // Check if any MyListing taxonomy was detected |
| 1025 | foreach($taxonomies as $taxonomy) { |
| 1026 | if(!empty($query[$taxonomy]) && empty($_GET[$taxonomy])) { |
| 1027 | return array( |
| 1028 | "page_id" => $explore_page_id, |
| 1029 | "explore_tab" => $taxonomy, |
| 1030 | "explore_{$taxonomy}" => $query['term'] |
| 1031 | ); |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | return $query; |
| 1037 | } |
| 1038 | |
| 1039 | /** |
| 1040 | * 13. bbPress |
| 1041 | */ |
| 1042 | function bbpress_endpoints($endpoints, $all = true) { |
| 1043 | $bbpress_endpoints = array(); |
| 1044 | $bbpress_endpoints[] = bbp_get_edit_slug(); |
| 1045 | // $bbpress_endpoints[] = bbp_get_paged_slug(); |
| 1046 | |
| 1047 | return ($all) ? $endpoints . "|" . implode("|", $bbpress_endpoints) : $bbpress_endpoints; |
| 1048 | } |
| 1049 | |
| 1050 | function bbpress_detect_endpoints() { |
| 1051 | global $wp_query; |
| 1052 | |
| 1053 | if(!empty($wp_query->query)) { |
| 1054 | $edit_endpoint = bbp_get_edit_slug(); |
| 1055 | |
| 1056 | if(isset($wp_query->query[$edit_endpoint])) { |
| 1057 | if(isset($wp_query->query['forum'])) { |
| 1058 | $wp_query->bbp_is_forum_edit = true; |
| 1059 | } else if(isset($wp_query->query['topic'])) { |
| 1060 | $wp_query->bbp_is_topic_edit = true; |
| 1061 | } else if(isset($wp_query->query['reply'])) { |
| 1062 | $wp_query->bbp_is_reply_edit = true; |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | /** |
| 1069 | * 14. Dokan |
| 1070 | **/ |
| 1071 | function dokan_endpoints($endpoints) { |
| 1072 | return "{$endpoints}|edit|edit-account"; |
| 1073 | } |
| 1074 | |
| 1075 | function dokan_detect_endpoints() { |
| 1076 | global $post, $wp_query, $wp, $pm_query; |
| 1077 | |
| 1078 | // Check if Dokan is activated |
| 1079 | if(!function_exists('dokan_get_option') || is_admin()) { return; } |
| 1080 | |
| 1081 | // Get Dokan dashboard page id |
| 1082 | $dashboard_page = dokan_get_option('dashboard', 'dokan_pages'); |
| 1083 | |
| 1084 | // Stop the redirect |
| 1085 | if(!empty($dashboard_page) && !empty($post->ID) && ($post->ID == $dashboard_page)) { |
| 1086 | $wp->query_vars['do_not_redirect'] = 1; |
| 1087 | |
| 1088 | // Detect Dokan shortcode |
| 1089 | if(empty($pm_query['endpoint'])) { |
| 1090 | $wp->query_vars['page'] = 1; |
| 1091 | } else if(isset($wp->query_vars['page'])) { |
| 1092 | unset($wp->query_vars['page']); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | // 2. Support "Edit Product" pages |
| 1097 | if(isset($wp_query->query_vars['edit'])) { |
| 1098 | $wp_query->query_vars['edit'] = 1; |
| 1099 | $wp_query->query_vars['do_not_redirect'] = 1; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * 15. GeoDirectory |
| 1105 | */ |
| 1106 | public function geodir_custom_fields($default_uri, $native_slug, $element, $slug, $native_uri) { |
| 1107 | global $permalink_manager_uris; |
| 1108 | |
| 1109 | // Use only for GeoDirectory post types & custom permalinks |
| 1110 | if(empty($element->post_type) || (strpos($element->post_type, 'gd_') === false) || $native_uri || !function_exists('geodir_get_post_info')) { return $default_uri; } |
| 1111 | |
| 1112 | // Get place info |
| 1113 | $place_data = geodir_get_post_info($element->ID); |
| 1114 | |
| 1115 | // A. Category |
| 1116 | if(strpos($default_uri, '%category%') !== false) { |
| 1117 | $place_category_terms = wp_get_object_terms($element->ID, 'gd_placecategory'); |
| 1118 | $place_category_term = (!is_wp_error($place_category_terms) && !empty($place_category_terms) && is_object($place_category_terms[0])) ? Permalink_Manager_Helper_Functions::get_lowest_element($place_category_terms[0], $place_category_terms) : ""; |
| 1119 | |
| 1120 | if(!empty($place_category_term)) { |
| 1121 | $place_category = Permalink_Manager_Helper_Functions::get_term_full_slug($place_category_term, $place_category_term, 2, false); |
| 1122 | $place_category = Permalink_Manager_Helper_Functions::sanitize_title($place_category, true); |
| 1123 | |
| 1124 | $default_uri = str_replace('%category%', $place_category, $default_uri); |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | // B. Country |
| 1129 | if(strpos($default_uri, '%country%') !== false && !empty($place_data->country)) { |
| 1130 | $place_country = Permalink_Manager_Helper_Functions::sanitize_title($place_data->country, true); |
| 1131 | $default_uri = str_replace('%country%', $place_country, $default_uri); |
| 1132 | } |
| 1133 | |
| 1134 | // C. Region |
| 1135 | if(strpos($default_uri, '%region%') !== false && !empty($place_data->region)) { |
| 1136 | $place_region = Permalink_Manager_Helper_Functions::sanitize_title($place_data->region, true); |
| 1137 | $default_uri = str_replace('%region%', $place_region, $default_uri); |
| 1138 | } |
| 1139 | |
| 1140 | // D. City |
| 1141 | if(strpos($default_uri, '%city%') !== false && !empty($place_data->city)) { |
| 1142 | $place_city = Permalink_Manager_Helper_Functions::sanitize_title($place_data->city, true); |
| 1143 | $default_uri = str_replace('%city%', $place_city, $default_uri); |
| 1144 | } |
| 1145 | |
| 1146 | return $default_uri; |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * 16. BasePress |
| 1151 | */ |
| 1152 | function kb_adjust_query($query, $old_query, $uri_parts, $pm_query, $content_type) { |
| 1153 | $knowledgebase_options = get_option('basepress_settings'); |
| 1154 | $knowledgebase_page = (!empty($knowledgebase_options['entry_page'])) ? $knowledgebase_options['entry_page'] : ''; |
| 1155 | |
| 1156 | // A. Knowledgebase category |
| 1157 | if(isset($query['knowledgebase_cat']) && !empty($pm_query['id']) && strpos($pm_query['id'], 'tax-') !== false) { |
| 1158 | $query['post_type'] = 'knowledgebase'; |
| 1159 | unset($query['taxonomy']); |
| 1160 | unset($query['term']); |
| 1161 | |
| 1162 | $term_id = intval(preg_replace("/[^0-9]/", "", $pm_query['id'])); |
| 1163 | $term = get_term($term_id); |
| 1164 | |
| 1165 | if(empty($term->parent)) { |
| 1166 | $query['is_knowledgebase_product'] = 1; |
| 1167 | } |
| 1168 | } |
| 1169 | // B. Knowledgebase main page |
| 1170 | else if(!empty($knowledgebase_page) && !empty($pm_query['id']) && $pm_query['id'] == $knowledgebase_page) { |
| 1171 | $query = array( |
| 1172 | 'page_id' => $knowledgebase_page |
| 1173 | ); |
| 1174 | } |
| 1175 | |
| 1176 | return $query; |
| 1177 | } |
| 1178 | |
| 1179 | /** |
| 1180 | * 17. Ultimate Member |
| 1181 | */ |
| 1182 | public function um_detect_extra_pages($uri_parts) { |
| 1183 | global $permalink_manager_uris; |
| 1184 | |
| 1185 | $request_url = trim("{$uri_parts['uri']}/{$uri_parts['endpoint_value']}", "/"); |
| 1186 | $um_pages = array( |
| 1187 | 'user' => 'um_user', |
| 1188 | 'account' => 'um_tab', |
| 1189 | ); |
| 1190 | |
| 1191 | // Detect UM permalinks |
| 1192 | foreach($um_pages as $um_page => $query_var) { |
| 1193 | $um_page_id = UM()->config()->permalinks[$um_page]; |
| 1194 | // Support for WPML/Polylang |
| 1195 | $um_page_id = (!empty($uri_parts['lang'])) ? apply_filters('wpml_object_id', $um_page_id, 'page', true, $uri_parts['lang']) : $um_page_id; |
| 1196 | |
| 1197 | if(!empty($um_page_id) && !empty($permalink_manager_uris[$um_page_id])) { |
| 1198 | $user_page_uri = preg_quote($permalink_manager_uris[$um_page_id], '/'); |
| 1199 | preg_match("/^({$user_page_uri})\/([^\/]+)?$/", $request_url, $parts); |
| 1200 | |
| 1201 | if(!empty($parts[2])) { |
| 1202 | $uri_parts['uri'] = $parts[1]; |
| 1203 | $uri_parts['endpoint'] = $query_var; |
| 1204 | $uri_parts['endpoint_value'] = sanitize_title($parts[2]); |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | return $uri_parts; |
| 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * 18. WooCommerce Subscriptions |
| 1214 | */ |
| 1215 | function fix_wcs_subscription_links($permalink, $post, $old_permalink) { |
| 1216 | if(!empty($post->post_type) && $post->post_type == 'product' && strpos($old_permalink, 'switch-subscription=') !== false) { |
| 1217 | $query_arg = parse_url($old_permalink, PHP_URL_QUERY); |
| 1218 | $permalink = "{$permalink}?{$query_arg}"; |
| 1219 | } |
| 1220 | |
| 1221 | return $permalink; |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * 19. Store Locator - CSV Manager |
| 1226 | */ |
| 1227 | public function wpsl_regenerate_after_import($meta_id, $post_id, $meta_key, $meta_value) { |
| 1228 | global $permalink_manager_uris; |
| 1229 | |
| 1230 | if(strpos($meta_key, 'wpsl_') !== false && isset($_POST['wpsl_csv_import_nonce'])) { |
| 1231 | $default_uri = Permalink_Manager_URI_Functions_Post::get_default_post_uri($post_id); |
| 1232 | |
| 1233 | if($default_uri) { |
| 1234 | $permalink_manager_uris[$post_id] = $default_uri; |
| 1235 | update_option('permalink-manager-uris', $permalink_manager_uris); |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | } |
| 1241 |