Advertisement
swte

Custom Warmup links

Mar 5th, 2020
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.88 KB | None | 0 0
  1. add_filter('swift_performance_warmup_urls_to_save', 'my_custom_warmup_likns');
  2.  
  3. function my_custom_warmup_likns(){
  4.             global $wpdb;
  5.  
  6.             $urls = $post_urls = $menu_elements = $archives = $already_added = array();
  7.  
  8.             set_transient('swift_performance_initial_prebuild_links', true, $timeout);
  9.  
  10.             // Home
  11.             if (Swift_Performance_Cache::is_object_cacheable(home_url())){
  12.                 $urls[Swift_Performance::get_warmup_id(home_url())] = trailingslashit(home_url());
  13.             }
  14.  
  15.             // Post types
  16.             $post_types = array();
  17.             foreach (Swift_Performance::get_post_types(Swift_Performance::get_option('exclude-post-types')) as $post_type){
  18.                 $post_types[] = "'{$post_type}'";
  19.  
  20.                 // Archive
  21.                 if (Swift_Performance::check_option('cache-archive',1)){
  22.                     $archive = get_post_type_archive_link( $post_type );
  23.                     if ($archive !== false){
  24.                         $url = get_post_type_archive_link( $post_type );
  25.                         if (Swift_Performance_Cache::is_object_cacheable($url)){
  26.                             $archives[Swift_Performance::get_warmup_id($url)] = $url;
  27.                         }
  28.                     }
  29.                 }
  30.  
  31.                 // Terms
  32.                 if (Swift_Performance::check_option('cache-terms',1)){
  33.                     $taxonomy_objects = get_object_taxonomies( $post_type, 'objects' );
  34.                     foreach ($taxonomy_objects as $key => $value) {
  35.                         $terms = get_terms($key);
  36.                         foreach ( $terms as $term ) {
  37.                             $url = get_term_link( $term );
  38.                             if (Swift_Performance_Cache::is_object_cacheable($url)){
  39.                                 $archives[Swift_Performance::get_warmup_id($url)] = $url;
  40.                             }
  41.                         }
  42.                     }
  43.                 }
  44.  
  45.             }
  46.  
  47.             $menu_item_ids  = $wpdb->get_col("SELECT meta_value FROM {$wpdb->postmeta} LEFT JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.meta_value WHERE meta_key = '_menu_item_object_id' AND post_type != 'nav_menu_item'");
  48.             $public_post_ids    = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_date > '2018-12-31' AND post_type IN(".implode(',', $post_types).") ORDER BY post_date DESC");
  49.  
  50.             $posts = array_merge((array)$menu_item_ids, (array)$public_post_ids);
  51.  
  52.             // WPML
  53.             if ((!defined('SWIFT_PERFORMANCE_WPML_WARMUP') || SWIFT_PERFORMANCE_WPML_WARMUP) && function_exists('icl_get_languages') && class_exists('SitePress')){
  54.                 if($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}icl_translations'") == $wpdb->prefix . 'icl_translations') {
  55.                     $translations = $wpdb->get_col("SELECT DISTINCT element_id FROM {$wpdb->prefix}icl_translations WHERE language_code != source_language_code AND source_language_code IS NOT NULL and element_type LIKE 'post_%'");
  56.                     $posts = array_diff($posts, $translations);
  57.                 }
  58.                 global $sitepress;
  59.                 $languages = icl_get_languages('skip_missing=0&orderby=KEY&order=DIR&link_empty_to=str');
  60.                 foreach ($languages as $language){
  61.                     $sitepress->switch_lang($language['code'], true);
  62.                     foreach ($posts as $post_id){
  63.                         wp_cache_flush();
  64.                         $permalink = get_permalink($post_id);
  65.                         if (Swift_Performance_Cache::is_object_cacheable($permalink, $post_id)){
  66.                             if (in_array($post_id, $menu_item_ids)){
  67.                                 $menu_elements[Swift_Performance::get_warmup_id($permalink)] = $permalink;
  68.                             }
  69.                             else {
  70.                                 $post_urls[Swift_Performance::get_warmup_id($permalink)] = $permalink;
  71.                             }
  72.                         }
  73.                     }
  74.                 }
  75.             }
  76.             else {
  77.                 foreach ($posts as $post_id){
  78.                     wp_cache_flush();
  79.                     $permalink = get_permalink($post_id);
  80.                     if (Swift_Performance_Cache::is_object_cacheable($permalink, $post_id)){
  81.                         if (in_array($post_id, $menu_item_ids)){
  82.                             $menu_elements[Swift_Performance::get_warmup_id($permalink)] = $permalink;
  83.                         }
  84.                         else {
  85.                             $post_urls[Swift_Performance::get_warmup_id($permalink)] = $permalink;
  86.                         }
  87.                     }
  88.                 }
  89.             }
  90.  
  91.             $urls = array_merge($urls, $menu_elements, $archives, $post_urls);
  92.  
  93.             // Limit pages
  94.             $urls = array_slice($urls, 0, SWIFT_PERFORMANCE_WARMUP_LIMIT);
  95.  
  96.             return $urls;
  97.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement