Exemple #1
0
/**
 * Returns array of theme groups
 *
 * @param string $theme_name
 * @return array
 */
function w3_get_theme_files($theme_name)
{
    $patterns = array('404', 'search', 'taxonomy(-.*)?', 'front-page', 'home', 'index', '(image|video|text|audio|application).*', 'attachment', 'single(-.*)?', 'page(-.*)?', 'category(-.*)?', 'tag(-.*)?', 'author(-.*)?', 'date', 'archive', 'comments-popup', 'paged');
    $templates = array();
    $theme = w3tc_get_theme($theme_name);
    if ($theme && isset($theme['Template Files'])) {
        $template_files = (array) $theme['Template Files'];
        foreach ($template_files as $template_file) {
            /**
             * Check file name
             */
            $template = basename($template_file, '.php');
            foreach ($patterns as $pattern) {
                $regexp = '~^' . $pattern . '$~';
                if (preg_match($regexp, $template)) {
                    $templates[] = $template_file;
                    continue 2;
                }
            }
            /**
             * Check get_header function call
             */
            $template_content = @file_get_contents($template_file);
            if ($template_content && preg_match('~\\s*get_header[0-9_]*\\s*\\(~', $template_content)) {
                $templates[] = $template_file;
            }
        }
        sort($templates);
        reset($templates);
    }
    return $templates;
}
 /**
  * Returns array of detected URLs for theme templates
  *
  * @param string $theme_name
  * @return array
  */
 function get_theme_urls($theme_name)
 {
     $urls = array();
     $theme = w3tc_get_theme($theme_name);
     if ($theme && isset($theme['Template Files'])) {
         $front_page_template = false;
         if (get_option('show_on_front') == 'page') {
             $front_page_id = get_option('page_on_front');
             if ($front_page_id) {
                 $front_page_template_file = get_post_meta($front_page_id, '_wp_page_template', true);
                 if ($front_page_template_file) {
                     $front_page_template = basename($front_page_template_file, '.php');
                 }
             }
         }
         $home_url = w3_get_home_url();
         $template_files = (array) $theme['Template Files'];
         $mime_types = get_allowed_mime_types();
         $custom_mime_types = array();
         foreach ($mime_types as $mime_type) {
             list($type1, $type2) = explode('/', $mime_type);
             $custom_mime_types = array_merge($custom_mime_types, array($type1, $type2, $type1 . '_' . $type2));
         }
         foreach ($template_files as $template_file) {
             $link = false;
             $template = basename($template_file, '.php');
             /**
              * Check common templates
              */
             switch (true) {
                 /**
                  * Handle home.php or index.php or front-page.php
                  */
                 case !$front_page_template && $template == 'home':
                 case !$front_page_template && $template == 'index':
                 case !$front_page_template && $template == 'front-page':
                     /**
                      * Handle custom home page
                      */
                 /**
                  * Handle custom home page
                  */
                 case $template == $front_page_template:
                     $link = $home_url . '/';
                     break;
                     /**
                      * Handle 404.php
                      */
                 /**
                  * Handle 404.php
                  */
                 case $template == '404':
                     $permalink = get_option('permalink_structure');
                     if ($permalink) {
                         $link = sprintf('%s/%s/', $home_url, '404_test');
                     } else {
                         $link = sprintf('%s/?p=%d', $home_url, 999999999);
                     }
                     break;
                     /**
                      * Handle search.php
                      */
                 /**
                  * Handle search.php
                  */
                 case $template == 'search':
                     $link = sprintf('%s/?s=%s', $home_url, 'search_test');
                     break;
                     /**
                      * Handle date.php or archive.php
                      */
                 /**
                  * Handle date.php or archive.php
                  */
                 case $template == 'date':
                 case $template == 'archive':
                     $posts = get_posts(array('numberposts' => 1, 'orderby' => 'rand'));
                     if (is_array($posts) && count($posts)) {
                         $time = strtotime($posts[0]->post_date);
                         $link = get_day_link(date('Y', $time), date('m', $time), date('d', $time));
                     }
                     break;
                     /**
                      * Handle author.php
                      */
                 /**
                  * Handle author.php
                  */
                 case $template == 'author':
                     $author_id = false;
                     if (function_exists('get_users')) {
                         $users = get_users();
                         if (is_array($users) && count($users)) {
                             $user = current($users);
                             $author_id = $user->ID;
                         }
                     } else {
                         $author_ids = get_author_user_ids();
                         if (is_array($author_ids) && count($author_ids)) {
                             $author_id = $author_ids[0];
                         }
                     }
                     if ($author_id) {
                         $link = get_author_posts_url($author_id);
                     }
                     break;
                     /**
                      * Handle category.php
                      */
                 /**
                  * Handle category.php
                  */
                 case $template == 'category':
                     $category_ids = get_all_category_ids();
                     if (is_array($category_ids) && count($category_ids)) {
                         $link = get_category_link($category_ids[0]);
                     }
                     break;
                     /**
                      * Handle tag.php
                      */
                 /**
                  * Handle tag.php
                  */
                 case $template == 'tag':
                     $term_ids = get_terms('post_tag', 'fields=ids');
                     if (is_array($term_ids) && count($term_ids)) {
                         $link = get_term_link($term_ids[0], 'post_tag');
                     }
                     break;
                     /**
                      * Handle taxonomy.php
                      */
                 /**
                  * Handle taxonomy.php
                  */
                 case $template == 'taxonomy':
                     $taxonomy = '';
                     if (isset($GLOBALS['wp_taxonomies']) && is_array($GLOBALS['wp_taxonomies'])) {
                         foreach ($GLOBALS['wp_taxonomies'] as $wp_taxonomy) {
                             if (!in_array($wp_taxonomy->name, array('category', 'post_tag', 'link_category'))) {
                                 $taxonomy = $wp_taxonomy->name;
                                 break;
                             }
                         }
                     }
                     if ($taxonomy) {
                         $terms = get_terms($taxonomy, array('number' => 1));
                         if (is_array($terms) && count($terms)) {
                             $link = get_term_link($terms[0], $taxonomy);
                         }
                     }
                     break;
                     /**
                      * Handle attachment.php
                      */
                 /**
                  * Handle attachment.php
                  */
                 case $template == 'attachment':
                     $attachments = get_posts(array('post_type' => 'attachment', 'numberposts' => 1, 'orderby' => 'rand'));
                     if (is_array($attachments) && count($attachments)) {
                         $link = get_attachment_link($attachments[0]->ID);
                     }
                     break;
                     /**
                      * Handle single.php
                      */
                 /**
                  * Handle single.php
                  */
                 case $template == 'single':
                     $posts = get_posts(array('numberposts' => 1, 'orderby' => 'rand'));
                     if (is_array($posts) && count($posts)) {
                         $link = get_permalink($posts[0]->ID);
                     }
                     break;
                     /**
                      * Handle page.php
                      */
                 /**
                  * Handle page.php
                  */
                 case $template == 'page':
                     $pages_ids = get_all_page_ids();
                     if (is_array($pages_ids) && count($pages_ids)) {
                         $link = get_page_link($pages_ids[0]);
                     }
                     break;
                     /**
                      * Handle comments-popup.php
                      */
                 /**
                  * Handle comments-popup.php
                  */
                 case $template == 'comments-popup':
                     $posts = get_posts(array('numberposts' => 1, 'orderby' => 'rand'));
                     if (is_array($posts) && count($posts)) {
                         $link = sprintf('%s/?comments_popup=%d', $home_url, $posts[0]->ID);
                     }
                     break;
                     /**
                      * Handle paged.php
                      */
                 /**
                  * Handle paged.php
                  */
                 case $template == 'paged':
                     global $wp_rewrite;
                     if ($wp_rewrite->using_permalinks()) {
                         $link = sprintf('%s/page/%d/', $home_url, 1);
                     } else {
                         $link = sprintf('%s/?paged=%d', 1);
                     }
                     break;
                     /**
                      * Handle author-id.php or author-nicename.php
                      */
                 /**
                  * Handle author-id.php or author-nicename.php
                  */
                 case preg_match('~^author-(.+)$~', $template, $matches):
                     if (is_numeric($matches[1])) {
                         $link = get_author_posts_url($matches[1]);
                     } else {
                         $link = get_author_posts_url(null, $matches[1]);
                     }
                     break;
                     /**
                      * Handle category-id.php or category-slug.php
                      */
                 /**
                  * Handle category-id.php or category-slug.php
                  */
                 case preg_match('~^category-(.+)$~', $template, $matches):
                     if (is_numeric($matches[1])) {
                         $link = get_category_link($matches[1]);
                     } else {
                         $term = get_term_by('slug', $matches[1], 'category');
                         if (is_object($term)) {
                             $link = get_category_link($term->term_id);
                         }
                     }
                     break;
                     /**
                      * Handle tag-id.php or tag-slug.php
                      */
                 /**
                  * Handle tag-id.php or tag-slug.php
                  */
                 case preg_match('~^tag-(.+)$~', $template, $matches):
                     if (is_numeric($matches[1])) {
                         $link = get_tag_link($matches[1]);
                     } else {
                         $term = get_term_by('slug', $matches[1], 'post_tag');
                         if (is_object($term)) {
                             $link = get_tag_link($term->term_id);
                         }
                     }
                     break;
                     /**
                      * Handle taxonomy-taxonomy-term.php
                      */
                 /**
                  * Handle taxonomy-taxonomy-term.php
                  */
                 case preg_match('~^taxonomy-(.+)-(.+)$~', $template, $matches):
                     $link = get_term_link($matches[2], $matches[1]);
                     break;
                     /**
                      * Handle taxonomy-taxonomy.php
                      */
                 /**
                  * Handle taxonomy-taxonomy.php
                  */
                 case preg_match('~^taxonomy-(.+)$~', $template, $matches):
                     $terms = get_terms($matches[1], array('number' => 1));
                     if (is_array($terms) && count($terms)) {
                         $link = get_term_link($terms[0], $matches[1]);
                     }
                     break;
                     /**
                      * Handle MIME_type.php
                      */
                 /**
                  * Handle MIME_type.php
                  */
                 case in_array($template, $custom_mime_types):
                     $posts = get_posts(array('post_mime_type' => '%' . $template . '%', 'post_type' => 'attachment', 'numberposts' => 1, 'orderby' => 'rand'));
                     if (is_array($posts) && count($posts)) {
                         $link = get_permalink($posts[0]->ID);
                     }
                     break;
                     /**
                      * Handle single-posttype.php
                      */
                 /**
                  * Handle single-posttype.php
                  */
                 case preg_match('~^single-(.+)$~', $template, $matches):
                     $posts = get_posts(array('post_type' => $matches[1], 'numberposts' => 1, 'orderby' => 'rand'));
                     if (is_array($posts) && count($posts)) {
                         $link = get_permalink($posts[0]->ID);
                     }
                     break;
                     /**
                      * Handle page-id.php or page-slug.php
                      */
                 /**
                  * Handle page-id.php or page-slug.php
                  */
                 case preg_match('~^page-(.+)$~', $template, $matches):
                     if (is_numeric($matches[1])) {
                         $link = get_permalink($matches[1]);
                     } else {
                         $posts = get_posts(array('pagename' => $matches[1], 'post_type' => 'page', 'numberposts' => 1));
                         if (is_array($posts) && count($posts)) {
                             $link = get_permalink($posts[0]->ID);
                         }
                     }
                     break;
                     /**
                      * Try to handle custom template
                      */
                 /**
                  * Try to handle custom template
                  */
                 default:
                     $posts = get_posts(array('pagename' => $template, 'post_type' => 'page', 'numberposts' => 1));
                     if (is_array($posts) && count($posts)) {
                         $link = get_permalink($posts[0]->ID);
                     }
                     break;
             }
             if ($link && !is_wp_error($link)) {
                 $urls[$template] = $link;
             }
         }
     }
     return $urls;
 }
 /**
  * Stylesheet filter
  *
  * @param $stylesheet
  * @return string
  */
 function stylesheet_preview($stylesheet)
 {
     w3_require_once(W3TC_LIB_W3_DIR . '/Request.php');
     $theme_name = W3_Request::get_string('w3tc_theme');
     $theme = w3tc_get_theme($theme_name);
     if ($theme) {
         return $theme['Stylesheet'];
     }
     return $stylesheet;
 }